# Sum of sum of first n natural numbers in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this problem to find the sum of sum of first n natural numbers, we will find the sum of all numbers from 1 to n and add them together to find the sum.

Let’s take an example to learn about the concept,

Input : 4
Output : 10
Explanation :
Sum of first 1 natural number = 1
Sum of first 2 natural number = 1 + 2 = 3
Sum of first 3 natural number = 1 + 2 +3 = 6
Sum of first 4 natural number = 1 + 2 + 3 + 4 = 10
Sum of sum of 4 natural number = 1 + 3 + 6 + 10 = 20

## Example

Live Demo

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

## Output

sum of sum first 4natural numbers is 20