Press "Enter" to skip to content

Sum of Square of First N Natural Numbers in C++

Finding sum of sequential numbers like natural numbers are common questions in programming practical examination. Here we are going to find the sum of square of N natural numbers without any formula using C++ code.

As we find the sum of natural numbers, here we follow same steps except calculating the sum. Instead of adding each number to the ‘sum’ variable, here we add their square.

C++ Code for finding Sum of Square of First N Natural Numbers

#include <iostream>
using namespace std;
int main()
    int limit;
    int sumOfSquare = 0;
    cout << "Enter the limit :";
    cin >> limit;
    for(int num = 1; num<=limit; num++)
        sumOfSquare = sumOfSquare + (num * num);
    cout<<"Sum of square of natural numbers upto " << limit << " is " << sumOfSquare;
    return 0;

Code Explanation:

Variable sumOfSquare is assigned as zero at declaration stage. Varibale limit accepts a number that we want to find sum of square upto this number.

On for loop, it starts from 1 and ends with our limit. On loop body, square of each number (squre = number * number) is added to the variable sumOfSquare.

Finally we display the square of sum after loop execution.


If we give limit as 3, then the sum of square of natural numbers upto 3 will be 14.

Sum of Square = (1 x 1) + ( 2 x 2) + (3 x 3 )
= 1 + 4 + 9
= 14

Sample Output:

Sum of square of N natural numbers in c+++

One Comment

  1. zortilonrel zortilonrel 20th April 2021

    I like this weblog so much, saved to favorites. “I don’t care what is written about me so long as it isn’t true.” by Dorothy Parker.

Leave a Reply