C Program Sum of Squares Series

By | March 11, 2020

C Program Sum of Squares Series

/*
  C program to calculate and print 
  sum of the series 1^2+2^2+3^2+...+n^2
  C program to print the sum of squares of 
  all natural numbers from 1 to n.
  */
 
#include<stdio.h>
     
int main()
{
    int i,n,sum;
     
    printf("Enter the value of n: ");
    scanf("%d",&n);
     
    sum=0;
     
    /* loop to calculate sum from 1 to n*/
    for(i=1;i<=n;i++)
        sum= sum+(i*i);
     
    /*display the sum on screen*/
     
    printf("The Sum of the series 1^2+2^2+3^2+...+%d^2 is: %d\n",n,sum);
     
    return 0;
}

Output

Enter the value of n: 5
The Sum of the series 1^2+2^2+3^2+…+5^2 is: 55

Leave a Reply

Your email address will not be published. Required fields are marked *