fork download
  1. //Matthew Santos CS1A Ch. 5, Pg. 294, #1
  2. /***********************************************
  3.  *
  4.  * SUM OF NUMBERS
  5.  * _____________________________________________
  6.  * For a positive integer, adds the sum of all
  7.  * the numbers from 1 to the number entered.
  8.  * _____________________________________________
  9.  * INPUT
  10.  * num: number entered
  11.  *
  12.  * OUTPUT
  13.  * sum: sum of all the numbers 1-num
  14.  ***********************************************/
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. int main() {
  19.  
  20. int num; //User inputted number
  21. int sum; //Sum of numbers
  22.  
  23. //Get number
  24. cin >> num;
  25.  
  26. //Determine if positive then calculate
  27. if (num >= 0)
  28. {
  29. for (int i = 0; i <= num; i++)
  30. {
  31. sum += i;
  32. }
  33. //Outputs sum
  34. cout << sum << endl;
  35. }
  36. else
  37. {
  38. cout << "Enter a positive number" << endl;
  39. }
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5308KB
stdin
7
stdout
28