fork download
  1. //Gabriel Camacho CSC5 Chapter 5, P. 294 #1
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Calculate Sum of Intergers
  6.  * ___________________________________________________________________________
  7.  * The purpose of this program is to ask the user
  8.  * for a positive integer and use a loop to calculate the
  9.  * sum of all integers from 1 up to the number entered,
  10.  * then display the total.
  11.  * __________________________________________________________________________
  12.  * Input
  13.  * Num: 1-50 Number inputed 1 through 50
  14.  *
  15.  * Output
  16.  * Sum :1 -50 the calaculated sum of the numbers 1 to what was inserted
  17.  *
  18.  *
  19.  ******************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. int number;
  26. int sum = 0;
  27.  
  28. cout << "Enter a positive integer: ";
  29. cin >> number;
  30.  
  31. while (number <= 0)
  32. {
  33. cout << "Please enter a positive integer: ";
  34. cin >> number;
  35. }
  36.  
  37. for (int i = 1; i <= number; i++)
  38. {
  39. sum += i;
  40. }
  41.  
  42. cout << "The sum of all integers from 1 to "
  43. << number << " is " << sum << endl;
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5288KB
stdin
50
stdout
Enter a positive integer: The sum of all integers from 1 to 50 is 1275