Advertisement
Sauka1337

Untitled

Sep 25th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.    
  6.     bool isIncorrect;
  7.     int n;
  8.  
  9.     do {
  10.         isIncorrect = false;
  11.  
  12.         cout << "Enter the value of n : ";
  13.         cin >> n;
  14.  
  15.         if (cin.fail()) {
  16.             cin.clear();
  17.             cin.ignore();
  18.             isIncorrect = true;
  19.         }
  20.  
  21.         if (!isIncorrect && (n <= 0)) {
  22.             isIncorrect = true;
  23.         }
  24.  
  25.         if (isIncorrect) {
  26.             cout << "Input Error. Check if the data is correct." << endl;
  27.         }
  28.  
  29.         while (cin.get() != '\n');
  30.     } while (isIncorrect);
  31.  
  32.     int sumOfNumbers = 0;
  33.     int i;
  34.  
  35.     for (i = 1; i<=n; i++){
  36.         sumOfNumbers += ((i-1)+i);
  37.     }
  38.  
  39.     cout << "Sum of all numbers = " << sumOfNumbers;
  40.  
  41.     return 0;
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement