// IC210 section 4002 // 9 September 2009 //This program computes the sum of numbers entered by user //We assume the user enters positive integers separated by space terminated by //one negative number //The negative number is just to signal the end of the input, //the negative number is not part of sum // Ex user input: 2 67 8 -5 // Example output: Sum is 77 #include using namespace std; int main() { //declare variables int sum; //to hold the sum int readnumber; //to hold one number from the user //Ask user for numbers cout << "Enter positive integers separated by space and terminated by a negative integer " << endl; //initialize variables -- very important sum = 0; cin >> readnumber; //read first number given by user //as long as the number is positive, add it to the sum and //read another number from the display while(readnumber >= 0) { sum = sum + readnumber; cin >> readnumber; } //output result cout << "Sum is " << sum; return 0; }