Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- int main()
- {
- system("chcp 1251 > nul");
- int n, sum;
- bool isNotCorrect;
- isNotCorrect = false;
- sum = 0;
- std::cout << "Введите n (количество суммируемых элементов): ";
- do {
- std::cin >> n;
- if (std::cin.fail() || (n < 1)) {
- isNotCorrect = true;
- std::cout << "Введите натуральное число!" << '\n';
- std::cin.clear();
- while (std::cin.get() != '\n');
- }
- else {
- isNotCorrect = false;
- }
- } while (isNotCorrect);
- n++;
- for (int i = 1; i < n; i++)
- if (i % 2)
- sum = sum - i * i;
- else
- sum = sum + i * i;
- std::cout << "Значение искомой суммы: " << sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement