Sum of Series||C language||programming_info ||Series problem 7

Calculate sum of series:
1+1/2+1/3+1/4+.....N
Solution using C language:
#include<stdio.h>
int main()
{
  int i, N;
  float sum=0;
  printf("Enter N:");
  scanf("%d", &N);
  if(N<1){
    printf("Enter value of N > 0.");
    exit(1);
  }
  for(i=1; i<=N; i++)
  {
   if(i==1)
   {
     sum=1;
     printf("Series = 1");
   }
   else
   {
     sum = sum + (float)1/i;
     printf(" + 1/%d", i);
   }
  }
  printf("\nSum of the series = %0.2f", sum);
  return 0; 
}
Output:

Enter N:5
Series = 1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of the series = 2.28 

Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support