Q:

Write a program in C# Sharp to display the n terms of harmonic series and their sum

0

Write a program in C# Sharp to display the n terms of harmonic series and their sum

Sample Output:

                                                                                                                                                               
Calculate the harmonic series and their sum:                                                                                                                   
----------------------------------------------                    
Input the number of terms : 6                                                                                                         
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +                                                                                                         
Sum of Series upto 6 terms : 2.45

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;  
public class Exercise19  
{  
    public static void Main()
{
   int i,n;
   double s=0.0;
   
	Console.Write("\n\n");
    Console.Write("Calculate the harmonic series and their sum:\n");
    Console.Write("----------------------------------------------");
    Console.Write("\n\n");   
   
   Console.Write("Input the number of terms : ");
   n= Convert.ToInt32(Console.ReadLine());    
   Console.Write("\n\n");
   for(i=1;i<=n;i++)
   {
     Console.Write("1/{0} + ",i);
     s+=1/(float)i;
   }
   Console.Write("\nSum of Series upto {0} terms : {1} \n",n,s);
 } 
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now