fork download
  1. //Nathan Dominguez CSC5 Chapter 5, P. 294, #1
  2. //
  3.  
  4. /*******************************************************************************
  5.  *
  6.  * Calculate the Sum of Numbers
  7.  * _____________________________________________________________________________
  8.  * This prorgam will take an input positive number and will loop to get the sum
  9.  * of all the numbers from 1 up to the input number. The program will not
  10.  * accept negative numbers that are input.
  11.  * _____________________________________________________________________________
  12.  * INPUT
  13.  * number : the number entered
  14.  *
  15.  * OUTPUT
  16.  * sum : the summation of all numbers stored
  17.  * FORMULA
  18.  * sum = sum + number
  19.  ******************************************************************************/
  20. #include <iostream>
  21. #include <iomanip>
  22. using namespace std;
  23. int main ()
  24. {
  25.  
  26. int sum = 0; //OUTPUT-total of all numbers from 1 to number
  27. int number; //INPUT-the number that was chosen
  28.  
  29. // display message
  30. // input number
  31. cout << "Enter a number greater than 0." << endl;
  32. cin >> number;
  33.  
  34. for (int counter = 1; counter <= number; counter++)
  35. // counter goes up by 1
  36. // calculates sum
  37. {
  38. sum = sum + counter;
  39. // counter++;
  40. }
  41.  
  42. if (number < 1)
  43. { // display "invalid" message
  44. cout << number << " is an invalid input." << endl;
  45. }
  46. else
  47. // display output message
  48. cout << "The sum of numbers 1 through " << number
  49. << " " << "is " << sum << "." << endl;
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5300KB
stdin
7
stdout
Enter a number greater than 0.
The sum of numbers 1 through 7 is 28.