fork download
  1. // Cenyao Huang CS1A Homework 5, p. 294, #1
  2.  
  3. /******************************************************************************************************
  4. * CALCULATE THE SUM OF INTEGERS
  5. *
  6. * This program calculates the sum of the integers from 1 up to the number that was input.
  7. *
  8. * Input
  9. * num : the value of the number that was input
  10. * start_num : the value of the
  11. * Output
  12. * the sum of the integers from 1 up to the number that was input
  13. *
  14. ***************************************************************************************************/
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. int main() {
  19. // Data dictionary
  20. int num, start_num, sum;
  21.  
  22. // Request a number
  23. cout << "Enter a positive integer value: ";
  24. cin >> num;
  25. cout << num <<"";
  26.  
  27. // Input validation
  28. if (num < 0)
  29. cout << endl << "Do not enter a negative number. Please try again.";
  30.  
  31. // Calculate sum
  32. sum = 0;
  33. for (start_num = 1; start_num <= num; start_num++)
  34. {
  35. cout << endl << start_num;
  36. sum += start_num;
  37. }
  38.  
  39. if (num >= 0)
  40. cout << endl << "sum = " << sum;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Enter a positive integer value: 0
sum = 0