46.5k views
18 votes
29. Write a program that asks to input any ten numbers and displays sum of them​

User Town
by
3.2k points

1 Answer

5 votes

Answer:

Here is the code for a classic C++ program that does it:

--------------------------------------------------------------------------------

#include <iostream>

using namespace std;

int main()

{

int sum = 0;

int n;

cout << "Input 10 numbers: " << endl;

for (int i = 0; i < 10; i++)

{

cin >> n;

sum += n;

}

cout << "Sum of the numbers: " << sum << endl;

}

--------------------------------------------------------------------------------

Step-by-step explanation:

I'm assuming you know what "include", "using namespace std" and "int main()" do, so I will skip over those.

First, we declare a variable "sum" and initialize it with 0 so we can add numbers to it later.

Then, we declare a variable "n" that will be set as the input of the user.

The "for-loop" will iterate ( go ) from 0 to 9, and will set the value of "n" as the input that is given -> "cin >> n;". After that, we add the value of "n" to the sum variable.

After "i" reaches 9, it will exit the loop and proceed to printing the sum of the numbers.

Hope it helped!

User Rvillablanca
by
3.2k points