Question #102601

Write a program that will calculate the sum of numbers from 1 to 15. If the user enter a negative number the program will stop. (Use for loop with while or do-while).

Expert's answer

#include <iostream>
using namespace std;


int main()
{
    double sum = 0;


    for(int i=0;i<15;i++)
    {
        double number;
        do
        {
            cout << "Enter a number: ";
            cin >> number;
            cout << endl;


            sum += number;
            i++;


            if (i == 15)
            {
                break;
            }
        }while(number>=0);
    }


    cout << "sum = " << sum << endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS