C++ Program to Find Sum of Square of n Natural Numbers

This C++ program will find the sum of squares of n natural numbers. Starting from 1, 2, 3, etc are called natural numbers.

For example:

n = 3

Then, Sum =  12 + 2+ 3= 14

#include<iostream>

using namespace std;

int main()
{
	unsigned long n,i,sum=0,d;
	cout<<"Enter any number:";
	cin>>n;

	for(i=1;i<=n;++i)
	{
		d=i*i;
		sum+=d;
	}

	cout<<"Sum="<<sum;
	return 0;
}

Output:

Enter any number: 50
Sum=42925

6 thoughts on “C++ Program to Find Sum of Square of n Natural Numbers”

  1. The code provided was useful in correcting the code i had written. Could you please provide the Algorithm for this program?
    Thanks in advance!!

Leave a Comment

Your email address will not be published. Required fields are marked *