Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- bool isIncorrect;
- int n;
- do {
- isIncorrect = false;
- cout << "Enter the value of n : ";
- cin >> n;
- if (cin.fail()) {
- cin.clear();
- cin.ignore();
- isIncorrect = true;
- }
- if (!isIncorrect && (n <= 0)) {
- isIncorrect = true;
- }
- if (isIncorrect) {
- cout << "Input Error. Check if the data is correct." << endl;
- }
- while (cin.get() != '\n');
- } while (isIncorrect);
- int sumOfNumbers = 0;
- int i;
- for (i = 1; i<=n; i++){
- sumOfNumbers += ((i-1)+i);
- }
- cout << "Sum of all numbers = " << sumOfNumbers;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement