Sum of squares of first n natural numbers | CPP program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... ; Function to calculate sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; int summation ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } int main ( ) { int n = 2 ; cout << summation ( n ) ; return 0 ; }