//Saliha Babar CS1A Chapter 5, Page 294, #1
//
/************************************************************************
*
* CALCULATE SUM OF NUMBERS
* ______________________________________________________________________
* This program allows user to enter positive number and calculates the
* sum of all numbers from 1 to number entered
*
* Calculation is based on the formula
* sum = 1 + 2 + ... + until the number entered
*________________________________________________________________________
* INPUT
* userNumber : user choice of positive number
* count : counter variable for userNumber
*
* OUTPUT
* sum : act as an accumulator
* sum of number 1 up to the number entered
* *********************************************************************/
#include <iostream>
using namespace std;
int main() {
int userNumber; // INPUT - user choice of positive number
int count; // Counter variable
int sum = 0; // Accumulator set to 0
// Get the input from the user
cout << "Enter any positive number and I will\n";
cout << "calculate the sum of numbers up to number entered.\n";
cout << "Enter your number below\n";
cin >> userNumber;
// Validate user input to only accept positive number
while (userNumber < 0)
{
cout << "You entered negative number\n";
cout << "Enter a postive number only\n";
cin >> userNumber;
}
// Start the loop while accumulating the sum tool
for (count = 1 ; count <= userNumber ; count ++)
{
sum += count;
}
// Display the output
cout << "The sum of number from 1 to " << userNumber << endl;
cout << "is " << sum << endl;
return 0;
}