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

To find out the sum of series 1+2+...+n

Solution using C language:

  1. #include<stdio.h>
  2. int main()
  3. int n,i;
  4. int sum=0;
  5. printf("Enter the n i.e. max values of series: ");
  6. scanf("%d",&n);
  7. sum = (n * (n + 1)) / 2;
  8. printf("Sum of the series: ");
  9. for (i =1;i <= n;i++) {
  10. if (i!=n)
  11. printf("%d + ",i);
  12. else
  13. printf("%d = %d ",i,sum);
  14. }
  15. return 0;
  16. }
Output:
Enter the n i.e. max values of series: 5
Sum of the series: 1 + 2 + 3 + 4 + 5 = 15
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