# C Program to Find Sum of Series 1^2 + 2^2 + …. + n^2

This is a C Program to find the sum of series 1^2 + 2^2 + …. + n^2.

Problem Description

This C Program calculates the Sum of Series 1^2 + 2^2 + …. + n^2.

Problem Solution

Then the Sum of the series 1^2 + 2^2 + …. + n^2 = n(n + 1)(2n + 1) / 6.

Program/Source Code

Here is source code of the C Program to Find the Sum of Series 1^2 + 2^2 + …. + n^2. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C Program to find the sum of series 1^2 + 2^2 + …. + n^2.
*/
#include <stdio.h>

int main()
{
int number, i;
int sum = 0;

printf("Enter maximum values of series number: ");
scanf("%d", &number);
sum = (number * (number + 1) * (2 * number + 1 )) / 6;
printf("Sum of the above given series : ");
for (i = 1; i <= number; i++)
{
if (i != number)
printf("%d^2 + ", i);
else
printf("%d^2 = %d ", i, sum);
}
return 0;
}```
Program Explanation

In this C Program, we are reading the limit to compute summation from the series 1^2 + 2^2 + …. + n^2 using ‘number’ integer variable. To compute the sum of series, the following formula is used

Sum of series = 1^2 + 2^2 + …. + n^2= n(n + 1)(2n + 1) / 6.

For loop is used to compute the sum of series. Initialize the value of ‘i’ variable as 1. Check the condition that the value of ‘i’ variable is less than or equal to the value of ‘number’ variable value. If the condition is true, then execute the iteration of the loop.

If-else condition statement is used to check that the value of ‘i’ variable is not equal to value of ‘number’ variable. If the condition is true, then execute the statement by only printing the value of ‘i’ variable. Otherwise, if the condition is false then execute the else statement and print the sum of series.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
```
Output:
\$ cc pgm18.c
\$ a.out
Enter maximum values of series number: 4
Sum of the above given series : 1^2 + 2^2 + 3^2 + 4^2 = 30```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms 