fork(1) download
  1. // Nicolas Ruano CS1A Pp.492 1
  2. /******************************************************************************
  3.  * SUM OF NUMBERS
  4.  *
  5.  * In this program, we are inputing the sum of numbers and getting the final
  6.  * result based on those calculations
  7.  *
  8.  * ****************************************************************************
  9.  * INPUT:
  10.  * Enter a positive integer -- type any number you want to solve with and then
  11.  * sum the value out
  12.  *
  13.  * Positive integer is 50
  14.  *
  15.  * OUTPUT:
  16.  * The mathematical opetation being solved with the sum of numbers from 1 to 50
  17.  * is: 1275
  18. ******************************************************************************/
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. int main() {
  23. int number;
  24. int sum = 0;
  25.  
  26. // Ask for input
  27. cout << "Enter a positive integer: 50";
  28. cin >> number;
  29.  
  30. // Input validation
  31. while (number < 0) {
  32. cout << "Invalid input! Please enter a positive integer: ";
  33. cin >> number;
  34. }
  35.  
  36. // Loop to calculate sum
  37. for (int i = 1; i <= number; i++) {
  38. sum += i;
  39. }
  40.  
  41. // Display result
  42. cout << "The sum of numbers from 1 to 50 " << number << " is: " << sum << endl;
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter a positive integer: 50The sum of numbers from 1 to 50 22015 is: 242341120