avr39ripe

sumFrom1to500Alt

Jun 28th, 2021
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int maxRange{ 500 };
  6.     int num;
  7.     bool correct{ false };
  8.  
  9.     int sum{ 0 };
  10.  
  11.     do
  12.     {
  13.         std::cout << "Enter number maximum 500\n";
  14.         std::cin >> num;
  15.         correct = num < maxRange;
  16.  
  17.         if (!correct)
  18.         {
  19.             std::cout << "Max range exceeded! Try again!";
  20.         }
  21.     } while (!correct);
  22.  
  23.     for (; num < maxRange; sum += num++);
  24.     std::cout << "Sum is: " << sum << '\n';
  25.  
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment