Write a program to generate Harmonic Series.
Input:- 5
Output:- 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately)
Sample Program:-
 /**  
  * @author Dixit  
  *   
  */  
 public class HarmonicSeries {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int num = 5;  
           double result = 0.0;  
           while (num > 0) {  
                result = result + (double) 1 / num;  
                num--;  
           }  
           System.out.println("Output is " + result);  
      }  
 }  
Output  
 Output is 2.283333333333333  
Enjoy Programming.
No comments:
Post a Comment