Lab2_T2_V10

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
965 B
3
Indexable
Never
#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;
    
    cout<<"\nInput x, n"<<endl;
    cin>>x>>n;
    
    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 p = 1;
    for (int i=1; i<=n; i++)
    {
        p *= Function(x, i);
    }
    return p;
}
double WhileSum(double x, int n)
{
    double p = 1;
    int i=1;
    while(i<=n)
    {
        p *= Function(x, i);
        i++;
    }
    return p;
}
double DoSum(double x, int n)
{
    double p = 1;
    int i=1;
    do
    {
        p *= Function(x, i);
        i++;
    } while(i<=n);
    return p;
}
double Function(double x, int i)
{
    return 2 * x + sqrt(i);
}