l2_t2

 avatar
unknown
c_cpp
2 years ago
1.1 kB
3
Indexable
#include <iostream>
#include <cmath>

using namespace std;

double ForSum(double x, int n);
double WhileSum(double x, int n);
double DoSum(double x, int n);
double Function(double x, int i);
int main()
{
    double x;
    int n;
    while(true)
    {
        cout<<"\nInput x, n"<<endl;
        cin>>x>>n;
        if (0<x && 0<n)break;
        cout<<"\nIncorrect input values "<< endl;
    }
    cout<<"Result with for ="<< ForSum(x, n)<<endl
    <<"Result with while ="<< WhileSum(x, n)<<endl
    <<"Result with do while ="<< DoSum(x, n)<<endl;
    return 0;
}

double ForSum(double x, int n)
{
    double s=0;
    for (int i=1; i<=n; i++)
    {
        s += Function(x, i);
    }
    return s;
}
double WhileSum(double x, int n)
{
    double s=0;
    int i=1;
    while(i<=n)
    {
        s += Function(x, i);
        i++;
    }
    return s;
}
double DoSum(double x, int n)
{
    double s=0;
    int i=1;
    do
    {
        s += Function(x, i);
        i++;
    }while(i<=n);
    return s;
}
double Function(double x, int i)
{
    return 2 / pow(x + i, 3);
}
Editor is loading...