fork download
  1. //Andres Guzman CSC5 Chapter 5, P. 294, #1
  2. //
  3. /*******************************************************************************
  4. *
  5. * DISPLAY SUM OF NUMBER
  6. * ______________________________________________________________________________
  7. * This program will display a sum of integers after a inputted integers
  8. *
  9. * Computation is based on the formula:
  10. *
  11. * Input Validation: num <= 0
  12. *
  13. * total += sum, sum++
  14. * ______________________________________________________________________________
  15. * INPUT
  16. *
  17. * num = The inputed integer by the programmer
  18. *
  19. * OUTPUT
  20. *
  21. * sum = All the integers prior to the input.
  22. * total = The added sums
  23. *
  24. *******************************************************************************/
  25. #include <iostream>
  26. using namespace std;
  27. int main ()
  28. {
  29. int num; // Holds the input integer
  30. int sum = 1; // counter starting from the first positive number
  31. int total = 0; //Accumulates the added total of sum
  32.  
  33. //
  34. //Display input prompt
  35. cout << "Enter a positive integer value: ";
  36. cin >> num;
  37. //
  38. //Initalize While Loop & nested While Loop
  39. while (num <= 0) //if the number is 0 or less, displays invalid.
  40. {
  41. cout << "\nInvalid value, please enter a positive integer value.\n";
  42. cout << "\nEnter a positive integer value: ";
  43. cin >> num;
  44. }
  45. while (sum <= num) //Sum will loop until it reachs the inputed integer
  46. {
  47. total += sum; //Adds up the sums into total for a final number
  48. sum++;
  49. }
  50. //
  51. //Output results
  52. cout << "\nSum: " << total; //Outputs the sum of all the numbers up to the
  53. //integer added
  54. }
Success #stdin #stdout 0s 5276KB
stdin
50
stdout
Enter a positive integer value: 
Sum: 1275