- Related Questions & Answers
- C++ Program for Sum of squares of first n natural numbers?
- Python Program for Sum of squares of first n natural numbers
- Java Program to calculate Sum of squares of first n natural numbers
- Sum of first n natural numbers in C Program
- Sum of sum of first n natural numbers in C++
- Difference between sum of the squares of and square of sum first n natural numbers.
- C Program for cube sum of first n natural numbers?
- C++ Program for cube sum of first n natural numbers?
- Sum of squares of the first n even numbers in C Program
- Program to find sum of first n natural numbers in C++
- Program for cube sum of first n natural numbers in C++
- Average of Squares of n Natural Numbers?
- C Program for the cube sum of first n natural numbers?
- Python Program for cube sum of first n natural numbers
- Java Program to cube sum of first n natural numbers

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The sum of squares of the first n natural numbers is found by adding up all the squares.

**Input **- 5**Output **- 55**Explanation **- 1^{2} + 2^{2} + 3^{2} + 4^{2} + 5^{2}

There are two methods to find the Sum of squares of first n natural numbers −

**Using Loops −** the code loops through the digits until n and find their square, then add this to a sum variable that outputs the sum.

#include <iostream> using namespace std; int main() { int n = 5; int sum = 0; for (int i = 1; i >= n; i++) sum += (i * i); cout <<"The sum of squares of first "<<n<<" natural numbers is "<<sum; return 0; }

The sum of squares of first 5 natural numbers is 55

**Using Formula **− To decrease the load on the program you can use mathematical formula to find the sum of squares on first n natural numbers. The mathematical formula is : n(n+1)(2n+1)/6

#include <stdio.h> int main() { int n = 10; int sum = (n * (n + 1) * (2 * n + 1)) / 6; printf("The sum of squares of %d natural numbers is %d",n, sum); return 0; }

The sum of squares of 10 natural numbers is 385

Advertisements