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

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

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 * n + 1 )) / 6;
  8. printf("Sum of the series : ");
  9. for (i =1;i<=n;i++) {
  10. if (i != n)
  11. printf("%d^2 + ",i); else
  12. printf("%d^2 = %d ",i,sum);
  13. }
  14. return 0;
  15. }

Output:
Enter the n i.e. max values of series: 5
Sum of the series: 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55
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