- C++ Programming Examples
- C++ Programming Examples
- C++ Print Hello World
- C++ Get Input from User
- C++ Print Integer
- C++ Add Two Numbers
- 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
- C++ Check Reverse equal Original
- C++ Add Subtract Multiply Divide
- C++ Make Calculator
- C++ Add Digits of Number
- C++ Find Average, Percentage
- C++ Calculate Arithmetic Mean
- C++ Calculate Student Grade
- C++ Print Multiplication Table
- C++ Print Prime Numbers
- C++ Add n Numbers
- C++ Interchange Digits of Number
- C++ Reverse a Number
- C++ Swap Two Numbers
- C++ Count Positive Negative Zero
- C++ Largest of Two Numbers
- C++ Largest of Three Numbers
- C++ Find Factorial of Number
- C++ Find HCF & LCM
- C++ Area, Perimeter of Square
- C++ Area, Perimeter of Rectangle
- C++ Area, Perimeter of Triangle
- C++ Area, Circumference of Circle
- C++ Convert Fahrenheit to Celsius
- C++ Convert Celsius to Fahrenheit
- C++ Print ASCII Value
- C++ Print Fibonacci Series
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Generate Armstrong Numbers
- C++ Find nCr and nPr
- 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++ Print Diamond Pattern
- C++ Print Floyd's Triangle
- C++ Print Pascal's Triangle
- C++ Print Smiling Face
- C++ 1D Array Program
- C++ Linear Search
- C++ Binary Search
- C++ Add Two Numbers using Pointer
- C++ Find Largest Element in Array
- C++ Find Smallest Element in Array
- 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++ 2D Array Programs
- C++ Add Two Matrices
- C++ Subtract Two Matrices
- C++ Transpose Matrix
- C++ Multiply Two Matrices
- C++ 3D Array 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++ 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++ Generate Random Numbers
- C++ Read a File
- C++ Write Content to File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Print Date/Time
- C++ Get IP Address
- C++ Shutdown, Restart Computer
- C++ More Programs
- C++ Frequency of Word
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

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.

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:

// C++ Program to Add n Numbers // ----codescracker.com---- #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 evalutes 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 updatation part, and increments the value of
**i**and again checks the condition - The condition again evalutes 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

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.

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:

// Sum of n Numbers using while Loop // ----codescracker.com---- #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.

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

// Sum of n Numbers using Array // ----codescracker.com---- #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

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:

// Sum of n Numbers using Function // ----codescracker.com---- #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.

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**

// Add n Natural Numbers in C++ // ----codescracker.com---- #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:

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.