C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)


In this tutorial, we will be discussing a program to find the sum of the given series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n).

For this, we will be given with the value of n and our task is to add up every term starting from the first one to find the sum of the given series.

Example

#include <iostream>
using namespace std;
//calculating the sum of the series
int calc_sum(int n) {
   int i;
   int sum = 0;
   for (i = 1; i <= n; i++)
   sum += (i * i);
   return sum;
}
int main() {
   int n = 7;
   int res = calc_sum(n);
   cout << res << endl;
   return 0;
}

Output

140

Updated on: 09-Jul-2020

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements