Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We also find the value of e using the Taylor\'s series expansion. The Taylor\'s

ID: 3607371 • Letter: W

Question

We also find the value of e using the Taylor's series expansion. The Taylor's Series expansion of e" is defined as inf n=0 Please make sure your program conforms to the following requirements 1. Write a function called reverse that takes a number as a parameter and returns the reversed number. (20 points) special difference of the number and returns it. (15 points) code from the class examples accurate to the 'nth' term. (20 points) 2. Write a function called difference that accepts a number as a parameter, calculates the 3. Write a function called factorial that returns the factorial of a number. You can use the 4. Write a function called sum that takes 'x' and a number 'n' as a parameter and calculates e" You can use the pow function to calculate x" 6. In the main function, accept a series of numbers from the user. Stop if the number entered is negative. Use the difference method to find the sum of the special differences and print it. (10 points) 7. Next, still in the main function, accept the values of 'x' and 'n' from the user and use the sum function to calculate the required value and print it. (5 points) 8. Make sure you add comments to explain your logic. (5 points)

Explanation / Answer

#include<iostream>
#include<math.h>
using namespace std;

// Function to receive a number as parameter and return the reverse of the number
int reverse(int no)
{
// variable to store reverse number.
// Initialize the rev to zero
int rev = 0;
// Loops till number is not zero
while(no != 0)
{
// Calculates the reverse
// Multiplies the rev with 10 and adds the remainder of the number
rev = rev * 10 + (no % 10);
// Calculates the quotient and stores it in no
no /= 10;
}// End of loop
// Returns the reverse number
return rev;
}// End of function

// Function to accept a number as parameter calculate and return the factorial
int factorial(int no)
{
// Variable fact to store factorial
// Initializes to one
int fact = 1;
// Loops from 1 to the number
for (int i = 1; i <= no; i++)
// Calculates the factorial by multiplying the fact value with loop value and stores it in fact
fact *= i;
// Returns the factorial
return fact;
}// End of function

// Function to calculate the difference
int difference(int x, int y)
{
// Checks if x value is greater than y value then return x - y
if(x > y)
return x - y;
// Otherwise return y - x value
else
return y - x;
}// End of function

// Function to calculate and return sum
int sum(int x, int n)
{
// To store sum. Initialized to zero
double sum = 0;
// To store power
double power;
// Loops from zero to n value
for(int c = 0; c <= n; c++)
{
// Calculates power. x is the base value and loop counter variable is power value
power = pow(x, c);
// Calculates sum by dividing power value by factorial of counter value
sum += power / factorial(c);
}// End of loop
// Returns the sum
return sum;
}// End of function

// main function definition
int main()
{
// Variable to store x and n value
int x, n;
// Loops till user enters negative number
do
{
// Accepts n value from the user
cout <<" Enter n value : ";
cin>>n;
// Checks if the number entered by the user is negative then stop
if(n < 0)
break;
// Accepts x value
cout <<" Enter x value : ";
cin>>x;
// Calls the function to calculate the sum of the series
cout<<" Sum of the series = "<<sum(x, n);
}while(1);
}// End of main function

Sample Run:


Enter n value : 2

Enter x value : 2

Sum of the series = 5
Enter n value : 2

Enter x value : 4

Sum of the series = 13
Enter n value : -1