fork download
  1. //Xinnuo Wang CS1A chapter 5 , P.294, #1
  2. //
  3. /*******************************************************************************
  4. *
  5. * Sum of Numbers
  6. *-------------------------------------------------------------------------------
  7. * This program accepts an intenger nember, sum all the integers from 1 to
  8. * it, and valid the input.
  9. *
  10. * computation is based on the formula:
  11. * sum = numb 1+ numb 2+...+num n
  12. *-------------------------------------------------------------------------------
  13. * INPUT
  14. * numb n : the asked and the max number
  15. *
  16. * OUTPUT
  17. * total : the sum of numbers from 1 to n.
  18. *
  19. *******************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. int numbN;
  26. int numb=0;
  27. int total=0;
  28. cout<< "Enter the max number to sum"<< endl;
  29. cin >> numbN;
  30. while (numbN<0)
  31. {
  32. cout<<"Invalid Input"<< endl;
  33. return 0;
  34. }
  35.  
  36. while(numb<=numbN)
  37. {
  38. total+=numb;
  39. numb++;
  40. }
  41. cout<< "The total is: "<< total<< endl;
  42. return 0;
  43. }
Success #stdin #stdout 0s 5312KB
stdin
10
stdout
Enter the max number to sum
The total is: 55