GTU Programming for Problem Solving Practical-25
/*25. Write a program to evaluate the series 1^2+2^2+3^2+……+n^2*/
#include <stdio.h>
#include <math.h>
int main(void)
{
int n, sum = 0;
printf("\nEnter the number which you wanted the series:");
scanf("%d", &n);
printf("\n0^2");
for (int i = 1; i <= n; i++)
{
printf(" + %d^2", i);
sum = sum + pow(i, 2);
}
printf(" =%d\n\n", sum);
return 0;
}
Output:
Enter the number till which you wanted the series:10
0^2 + 1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 =385
0 Comments