Problem 25

author
By Supreme2023-03-15
Problem Statement :
Write a program to evaluate the series 1^2+2^2+3^2+……+n^2
Code:

#include <stdio.h>

int main(){
//25. Write a program to evaluate the series 1^2 +2^2 +3^2 +...... +n^2.

int sum=0, i, n;

printf("\n\tEvaluate The Series 1^2+2^2+3^2+......+n^2");
printf("\n===========================================================");

printf("\n\nEnter The Value Of N : ");
scanf("%d",&n);

for(i=1; i<=n; i++){
sum = (sum + (i*i));
}

printf("\n___________________________________________________________");
printf("\n\nSum Of %d Terms of This Series = %d",n,sum);
printf("\n___________________________________________________________\n\n");

return 0;
}

Description :
  • The program is intended to evaluate the series 1^2 +2^2 +3^2 +...... +n^2 and output the sum of the series.
  • The program first declares three integer variables: sum, i, and n.
  • The program then prints out a header and prompts the user to input the value of n using scanf.
  • The program uses a for loop to calculate the sum of the series. The loop iterates from i=1 to i=n, incrementing i by 1 at each iteration.
  • Within the loop, the value of i is squared and added to the current value of sum.
  • After the loop is complete, the program prints out the sum of the series using printf.
  • Finally, the program returns 0 to indicate successful completion.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.