
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sum of sum of first n natural numbers in C++
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
#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
- Related Articles
- Sum of first n natural numbers in C Program
- Sum of square-sums of first n natural numbers
- Find the sum of first $n$ odd natural numbers.
- Finding sum of first n natural numbers in PL/SQL
- Sum of squares of first n natural numbers in C Program?
- Python Program for cube sum of first n natural numbers
- C++ Program for cube sum of first n natural numbers?
- C Program for cube sum of first n natural numbers?
- Java Program to cube sum of first n natural numbers
- Python Program for Sum of squares of first n natural numbers
- C++ Program for Sum of squares of first n natural numbers?
- Program for cube sum of first n natural numbers in C++
- Program to find sum of first n natural numbers in C++
- Difference between sum of the squares of and square of sum first n natural numbers.
- 8085 program to find the sum of first n natural numbers

Advertisements