# C++ Program to Calculate Sum of Series 1²+2²+3²+n²

Write a C++ Program to Calculate Sum of Series 1²+2²+3²+n² with an example. The math formula to find Sum of Series 1²+2²+3²+…+n² = n(n + 1)(2n + 1)/6.

```#include<iostream>

using namespace std;

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

cout << "Please Enter the Number to find sum of Series 1^2 + 2^2 =  ";
cin >> number;

sum = (number * (number + 1) * (2 * number + 1 )) / 6;

cout << "\nThe Sum of the Series of " << number << " number = " << sum ;

return 0;
}```
``````Please Enter the Number to find sum of Series 1^2 + 2^2 =  10

The Sum of the Series of 10 number = 385``````

This below shown the C++ Program to Calculate Sum of Series 1²+2²+3²+n² is the same as the above. Here, we used C++ for loop to display the 1²+2²+3²+n² series as an output.

```#include<iostream>

using namespace std;

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

cout << "Please Enter the Number to find sum of Series 1^2 + 2^2 =  ";
cin >> number;

sum = (number * (number + 1) * (2 * number + 1 )) / 6;

cout << "\nThe Sum of the Series of " << number << " number = " << sum << "\n\n";

for(int i = 1; i <= number; i++)
{
if (i != number)
cout << i << "^2 + ";
else
cout << i << "^2 = " << sum;
}

return 0;
}```

## C++ Program to Calculate Sum of Series 1²+2²+3²+n² using Functions

```#include<iostream>

using namespace std;

void sumOfSeries (int number)
{
int sum = 0;

sum = (number * (number + 1) * (2 * number + 1 )) / 6;

cout << "\nThe Sum of the Series of " << number << " number = " << sum << "\n\n";

for(int i = 1; i <= number; i++)
{
if (i != number)
cout << i << "^2 + ";
else
cout << i << "^2 = " << sum;
}
}
int main()
{
int number;

cout << "Please Enter the Number to find sum of Series 1^2 + 2^2 =  ";
cin >> number;

sumOfSeries (number);

return 0;
}```
``````Please Enter the Number to find sum of Series 1^2 + 2^2 =  20

The Sum of the Series of 20 number = 2870

1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2 + 13^2 + 14^2 + 15^2 + 16^2 + 17^2 + 18^2 + 19^2 + 20^2 = 2870`````` Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.