Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #define ABS(x) ((x) < 0 ? (-(x)) : (x))
- // Цикл
- int main() {
- setlocale(LC_ALL, "Russian");
- // Расчет суммый от 1 до N элемента
- {
- unsigned int n;
- std::cout << "Введите N: ";
- std::cin >> n;
- double s = 0;
- for (unsigned int i = 1; i <= n; i++) {
- int factorial = 1;
- for (int f = 2; f <= i; f++) {
- factorial *= f;
- }
- double value = ((i % 2 == 0) ? 1. : -1.) / (double)factorial;
- s += value;
- }
- std::cout << "Сумма: " << s << std::endl;
- }
- // Расчет бесконечной суммы с точностью
- {
- double t;
- std::cout << "Введите точность: ";
- std::cin >> t;
- double s = 0;
- double value;
- unsigned i = 0;
- do {
- i++;
- int factorial = 1;
- for (int f = 2; f <= i; f++) {
- factorial *= f;
- }
- value = ((i % 2 == 0) ? 1. : -1.) / (double)factorial;
- s += value;
- } while (ABS(value) >= ABS(t));
- std::cout << "Сумма: " << s << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment