In this article, we will learn the C++ program to add N numbers. For example, if the user sets n=5, the program will take 5 values from the user and add them.
Example: C++ program to add N numbers
//C++ program to add N numbers
#include<iostream>
using namespace std;
int main()
{
int n, i, numbers, sum=0;
cout<<"Enter N numbers: ";
cin>>n;
cout<<"Enter the values for "<<n<<" numbers:\n";
for(i=0; i<n; i++)
{
cin>>numbers;
sum = sum+numbers;
}
cout<<"\nSum will be: "<<sum<<endl;
return 0;
}
Output
Description and working of this program
- The system asks the user to set n numbers for example n=5
- The user enters the 5 values against each n number.
- The program adds them and prints the sum on the screen