Write a function that returns a structure containing A B of

Write a function that returns a structure containing {A, B} of the type \'double.\' Inside the function itself the following are being calculated: Show whole program in C++ A = x + x^3/3! + x^5/5! + x^7/7! + x^9/9! + ... B = -x + x^2/2! - x^3/3! + x^4/4! - x^5/5! + ..

Solution

#include<iostream>
#include<math.h>
using namespace std;
//Find factorial
double fact(double n)
{
    double fac = 1;
   for(int c = 2; c <= n; c++)
     fac = fac * c;
    return fac;
}
//Series A
double Ser1(double n, double x)
{
    double A, p, f;
    A = x;
    for(int c = 3; c <= n; c+=2)
    {
        p = pow(x,c);
        f = fact(c);
        A = A + p / f;
    }
    return A;
}
//Series B
double Ser2(double n, double x)
{
    double B, p, f;
    B = -x;
    for(int c = 2; c <= n; c++)
    {
        p = pow(x,c);
        f = fact(c);
        //For + or - sign
        if(c % 2 == 0)
            B = B + p / f;
        else
            B = B - p / f;
    }
    return B;
}
int main()
{
    double x, n;
    //Accept number of terms
    cout<<\"\ Enter the value of n: \";
    cin>>n;
    //Accept x value
    cout<<\"\ Enter the value of x: \";
    cin>>x;

    cout<<\"\ A = \"<<Ser1(n, x);

    cout<<\"\ B = \"<<Ser2(n, x);
}


Outout:


Enter the value of n: 5

Enter the value of x: 2

A = 3.6
B = -0.933333

 Write a function that returns a structure containing {A, B} of the type \'double.\' Inside the function itself the following are being calculated: Show whole p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site