fork download
  1. //Andrew Alspaugh CS1A CHAPTER 5. P. 294 #1
  2. //
  3. /***************************************************************************
  4.  * Compute Sum of Numbers
  5.  * ________________________________________________________________________
  6.  * The purpose of this program is to compute the sum of integer values between 1
  7.  * and the entered value
  8.  *_________________________________________________________________________
  9.  * INPUT:
  10.  * INTEGER
  11.  * OUTPUT:
  12.  * sum
  13.  **************************************************************************/
  14. #include <iostream>
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19. //INPUT WITH VALIDATION
  20. int INTEGER;
  21.  
  22. cout << "Enter a positive integer" << endl;
  23. cin >> INTEGER;
  24. cout << "You entered: " << INTEGER << endl;
  25. while (INTEGER <= 0)
  26. {
  27. cout << "Error: Enter Integer greater than 0:" << endl;
  28. cin >> INTEGER;
  29. cout << "You Entered: " << INTEGER << endl;
  30. }
  31.  
  32. //CALCULATE SUM OF NUMBERS
  33. int sum = 0;
  34. int num = 1;
  35.  
  36. do
  37. {
  38. sum += num;
  39. num++;
  40. }
  41. while (num <=INTEGER);
  42.  
  43. //DISPLAY SUM OF INTEGERS
  44. cout << "Sum of Integers is: " << sum << endl;
  45.  
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 5324KB
stdin
0
50
stdout
Enter a positive integer
You entered: 0
Error: Enter Integer greater than 0:
You Entered: 50
Sum of Integers is: 1275