Write a program to get number from user until user enter a negative number and show running sum of all the user input.
#include <iostream>
using namespace std;
int main() {
int sum = 0, count = 0, input; // initialize all variables
while (true) { // loop forever (until break is reached below)
cin >> input; // get user input
if (input < 0) break; // if it's negative, exit loop
sum += input; // otherwise add it to the running sum
count++; // increase the count by 1
}
if (count > 0) cout << (double)sum;
else cout << "No numbers" << endl;
return 0;
}
Comments
Leave a comment