fork download
  1. // Drew Duffey CSC5 Chapter 5, P.294, #1
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * CALCULATE SUM
  6.  * _____________________________________________________________________________
  7.  * This program will calculate the sum of all numbers up to a specific positive
  8.  * number, also known as a termial.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  * number : The number the user wants to find the termial of
  12.  *
  13.  * OUTPUT
  14.  * sum : The sum of all the positive numbers below and including the number
  15.  *
  16. *******************************************************************************/
  17. #include <iostream>
  18. #include <iomanip>
  19. using namespace std;
  20. int main ()
  21. {
  22. int number; //INPUT - The number the user wants to find the terminal value
  23. //of
  24. float sum; //OUTPUT - The sum of all the positive numbers below and
  25. //including the number
  26.  
  27. //Prompt the user to input a positive number, giving an error if the number
  28. //is negative.
  29. cout << "Please enter a positive whole integer: ";
  30. cin >> number;
  31.  
  32. //Input Validation
  33. if (number < 0)
  34. {
  35. cout << "\nInvalid number. Please try again.";
  36. return 0;
  37. }
  38.  
  39. //Calculate sum
  40. sum = (number*(number+1))/2;
  41. cout << "\nThe termial of that number is " << sum;
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 5320KB
stdin
72
stdout
Please enter a positive whole integer: 
The termial of that number is 2628