Answer to Question #196625 in C++ for M Kamran

Question #196625

Write a program to get number from user until user enter a negative number and show running sum of all the user input.


1
Expert's answer
2021-05-21T07:23:10-0400
#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;
}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog