fork download
  1. //Cecilia Rangel CSC5 Chapter 5, P.294, #1
  2. //
  3. /**************************************************************
  4.  *
  5.  * SUM OF NUMBERS
  6.  * ____________________________________________________________
  7.  * This program will ask for the user to input a positive
  8.  * number, and the program adds up all numbers between 0
  9.  * and the number inputted
  10.  *
  11.  *_____________________________________________________________
  12.  * INPUT
  13.  * user input : a number
  14.  *
  15.  * OUTPUT
  16.  * totalSum : sum of all number 0 - user input
  17.  *
  18.  *
  19.  **************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. int main() {
  24. int integer; //user input
  25. int counter; //counter variable
  26. int totalSum; //output sum of integers
  27.  
  28. //ask user for integer
  29. cout << "Enter postive integer: ";
  30. cin >> integer;
  31. cout << integer << endl;
  32.  
  33. if (integer >= 0)
  34. {
  35. while (counter != integer)
  36. {
  37. counter++;
  38. totalSum += counter;
  39. }
  40.  
  41. cout << "sum of numbers: " << totalSum;
  42. }
  43. else
  44. cout << "Invalid. Positive integer only.";
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5288KB
stdin
5
stdout
Enter postive integer: 5
sum of numbers: 15