- C++ Programming Examples
- C++ Programming Examples
- C++ Hello World
- C++ Get Input
- C++ Print Integer
- C++ Add Two Numbers
- C++ Add Sub Mul Div
- C++ Add Digits
- C++ Find Average Perc
- C++ Find Arithmetic Mean
- C++ Sum of n Natural Numbers
- C++ Sum of n Numbers
- C++ Area Perimeter of Square
- C++ Area Perimeter of Rectangle
- C++ Area Perimeter of Triangle
- C++ Area Circum of Circle
- C++ Find Simple Interest
- C++ Fahrenheit to Celsius
- C++ Celsius to Fahrenheit
- C++ Print Prime Numbers
- C++ Reverse a Number
- C++ Swap Two Numbers
- C++ Print Multiplication Table
- C++ Find Factorial of Number
- C++ Find Factors of Number
- C++ Find HCF & LCM
- C++ Make Calculator
- C++ Count Digits in Number
- C++ Sum of First & Last Digit
- C++ Product of Digits of Number
- C++ Sum of Squares of Digits
- C++ Interchange Digits of Number
- C++ if else Programs
- C++ Check Even or Odd
- C++ Check Prime or Not
- C++ Check Alphabet or Not
- C++ Check Vowel or Not
- C++ Check Leap Year or Not
- Check Reverse equal Original
- C++ Check Perfect Number
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Divisibility Test
- C++ Find Wage of Labor
- C++ Find Discounted Price
- C++ Find Shipping Charge
- C++ Find Telephone Bills
- C++ Calculate Student Grade
- C++ Largest of Two Numbers
- C++ Largest of Three Numbers
- C++ Number Conversion
- C++ Decimal to Binary
- C++ Decimal to Octal
- C++ Decimal to Hexadecimal
- C++ Binary to Decimal
- C++ Binary to Octal
- C++ Binary to Hexadecimal
- C++ Octal to Decimal
- C++ Octal to Binary
- C++ Octal to Hexadecimal
- C++ Hexadecimal to Decimal
- C++ Hexadecimal to Binary
- C++ Hexadecimal to Octal
- C++ Pattern Programs
- C++ Pattern Programs
- C++ Print Diamond Pattern
- C++ Print Floyd's Triangle
- C++ Print Pascal's Triangle
- C++ Array Programs
- C++ 1D Array Program
- C++ Linear Search
- C++ Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Find Second Largest Element
- Find Second Smallest Element
- C++ Sum of All Elements
- C++ Multiply All Elements
- C++ Element on Even Position
- C++ Element on Odd Position
- C++ Print Even Numbers in Array
- C++ Print Odd Numbers in Array
- C++ Count Even/Odd Numbers
- C++ Sum of Even/Odd Numbers
- C++ Count Positive Negative Zero
- C++ Reverse an Array
- C++ Insert Element in Array
- C++ Delete Element from Array
- C++ Merge two Arrays
- C++ Bubble Sort
- C++ Selection Sort
- C++ Insertion Sort
- C++ Common Elements
- C++ 2D Array Programs
- C++ Add Two Matrices
- C++ Subtract Two Matrices
- C++ Transpose Matrix
- C++ Multiply Two Matrices
- C++ 3D Array Programs
- C++ String Programs
- C++ Print String
- C++ Find Length of String
- C++ Compare Two Strings
- C++ Copy String
- C++ Concatenate String
- C++ Reverse a String
- C++ Delete Vowels from String
- C++ Delete Word from String
- C++ Count Character in String
- C++ Count Word in String
- C++ Frequency of Word
- C++ Remove Spaces from String
- C++ Sort a String
- C++ Uppercase to Lowercase
- C++ Lowercase to Uppercase
- C++ Swap Two Strings
- C++ Check Anagram or Not
- C++ Capitalize All Words in String
- C++ Capitalize Specific Character
- C++ Get Numbers from String
- C++ File Programs
- C++ Read a File
- C++ Write Content to File
- C++ Append Data in File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- Count Characters, Words in File
- C++ Capitalize All Words in File
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Misc Programs
- C++ Print ASCII Value
- C++ Add Binary Numbers
- C++ Generate Random Numbers
- C++ Print Smiling Face
- C++ Days into Years, Months
- Add Two Numbers using Pointer
- C++ Print Fibonacci Series
- Generate Armstrong Numbers
- C++ Find nCr and nPr
- C++ Get IP Address
- C++ Print Date/Time
- C++ Shutdown, Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List

# C++ Program to Add n Numbers

In this article, you will learn and get code on adding of **n** numbers (or finding sum of **n** numbers)
given by user at run-time using C++ program. For example, if user enters the value of **n** as **5**. Then the
program further asks to enter any 5 numbers. So that it can add all the 5 entered numbers and print its summation
result as output.

Here are the list of approaches that are used to do the task of adding **n** numbers:

- Find sum of n numbers using for loop
- using while loop
- using array
- using user-defined function

At last of the article, you will also see a program that adds **n** natural numbers.

To add n numbers in C++ programming, you have to ask from user to enter the value of
**n** (i.e., how many numbers he/she wants to enter), then ask to enter **n** numbers to perform the addition
of all the given numbers and finally display the result on the screen as shown here in the following program.

## Find Sum of n Numbers using for Loop

This program is created using **for** loop to do the job. The question is, **write a program in C++ to find
and print the sum of n numbers using for loop.** And here is the answer to this question:

#include<iostream> using namespace std; int main() { int i, n, num, sum=0; cout<<"How many numbers you want to enter ? "; cin>>n; cout<<"Enter "<<n<<" numbers: "; for(i=0; i<n; i++) { cin>>num; sum = sum+num; } cout<<"\nSum of all "<<n<<" numbers is "<<sum; cout<<endl; return 0; }

This program was build and run under *Code::Blocks* IDE. Here is its sample run:

Now supply or enter the value of **n** (that how many numbers you wants to enter and add them up) say **5**.
And then enter any 5 numbers. Press `ENTER`

key to see the following output, that will be the
summation of all the 5 numbers entered by user at run-time:

The program is very simple. That is, received the value of **n** first, and then created a **for loop** that
runs **n** number of times to get all the numbers and add them up one by one. Therefore, if user enters the value
of **n** as 5, then the dry run of above program goes like:

- Inside
*for loop*, initially 0 gets initialized to**i**and checks the condition, that is whether the value of**i**is less than**n**'s value or not - Condition evaluates to be true, therefore program flow goes inside the loop, and receives a number inside a
variable say
**num** - And
**sum+num**gets initialized to**sum**every time after receiving the number - Because, the initial value of
**sum**is 0, therefore at first execution of**for loop**, suppose user enters first number as 10. Therefore,**0+10**or**10**gets initialized to**sum**. Now**sum=10** - Program flow goes to update part, and increments the value of
**i**and again checks the condition - The condition again evaluates to be true for second time, and program flow again goes inside the loop, and
executes the two statements. That is, receives a number say
**20**and initializes**sum+20**or**10+20**as the new value of**sum**. Therefore, now**sum=30** - In similar way, process the operation
- The loop executes terminates, when its condition evaluated to be false
- After exiting from the loop, we'll have a variable
**sum**that holds the summation value of all the**n**numbers entered by user - Therefore, simply prints its value as output

### What if Number(s) contains Decimal ?

To handle with real numbers (numbers having decimal part), use the **float** data type instead of **int**.
Therefore just replace the following statement as given in above program:

int i, n, num, sum=0;

with the statement given below:

float i, n, num, sum=0;

Rest of the things will be same.

## Sum of n Numbers using while Loop

The question is, **write a program in C++ that find and prints the sum of n given numbers using while loop.**
The answer to this question is given below:

#include<iostream> using namespace std; int main() { int n, i=0, num, sum=0; cout<<"Enter the value of n: "; cin>>n; cout<<"Enter "<<n<<" numbers: "; while(i<n) { cin>>num; sum = sum+num; i++; } cout<<"\nSum = "<<sum; cout<<endl; return 0; }

The output produced by above program looks like:

Unlike **for loop**, in **while loop** we have to initialize the __loop variable__ before the start
of *while loop*. As it only contains the condition statement, therefore we also have to place the updatation part
inside the loop's body. Therefore, the value of **i** gets initialized before the loop and its value gets updated
using the last statement of its body.

## Add n Numbers using Array

This program uses array to do the same job as of previous program.

#include<iostream> using namespace std; int main() { int n, i, arr[50], sum=0; cout<<"Enter the value of n (max. 50): "; cin>>n; cout<<"Enter "<<n<<" numbers: "; for(i=0; i<n; i++) { cin>>arr[i]; sum = sum+arr[i]; } cout<<"\nSum = "<<sum; cout<<endl; return 0; }

Here is its sample run with same input as of previous output:

All the numbers gets stored inside the array in a way that:

- First number gets stored at
**arr[0]** - Second number at
**arr[1]** - Third at
**arr[2]** - and so on

## Add n Numbers using Function

This is the last program on adding **n** numbers. This program is created using a user-defined function named
**findSum()** to do the same job. This function receives array and its size as its two argument or parameters.
The array are the list of numbers, and size is the value of **n**. Further it finds summation of all numbers in
the list and returns it. That is, its return value gets initialized to **sum** inside the **main()** function:

#include<iostream> using namespace std; int findSum(int [], int); int main() { int n, i, arr[50], sum; cout<<"Enter the value of n (max. 50): "; cin>>n; cout<<"Enter "<<n<<" numbers: "; for(i=0; i<n; i++) cin>>arr[i]; sum = findSum(arr, n); cout<<"\nSum = "<<sum; cout<<endl; return 0; } int findSum(int arr[], int n) { int i, sum=0; for(i=0; i<n; i++) sum = sum+arr[i]; return sum; }

This will produce the same output as of previous one.

## Find Sum of n Natural Numbers in C++

This program adds first **n** natural number. The value of **n** must be entered by user. For example,
if the value of **n** gets entered by user as **5**. Then the following program will find out the sum of
first 5 natural numbers. That will be **1, 2, 3, 4, 5**. Therefore, its summation will be **1+2+3+4+5** equals
**15**

#include<iostream> using namespace std; int main() { int i, n, sum=0; cout<<"Enter the value of n: "; cin>>n; for(i=1; i<=n; i++) sum = sum+i; cout<<"\nSum of first "<<n<<" natural numbers = "<<sum; cout<<endl; return 0; }

The sample run of above program with user input as **10** will looks like:

#### Same Program in Other Languages

« Previous Program Next Program »

Like/Share Us on Facebook 😋