Respuesta :
Answer:
Here are the programs. I am writing C++ and Python programs:
a)
C++
#include<iostream>
using namespace std;
int factorial(int number) {
if (number == 0)
return 1;
return number * factorial(number - 1); }
int main() {
int integer;
cout<<"Enter a non negative integer: ";
cin>>integer;
cout<< "Factorial of "<< integer<<" is "<< factorial(integer)<< endl; }
Python:
def factorial(number):
if number == 0:
return 1
return number * factorial(number-1)
integer = int(input("Enter a non negative integer: "))
print("Factorial of", integer, "is", factorial(integer))
b)
C++
#include <iostream>
using namespace std;
double factorial(int number) {
if (number == 0)
return 1;
return number * factorial(number - 1); }
double estimate_e(int num){
double e = 1;
for(int i = 1; i < num; i++)
e = e + 1/factorial(i);
cout<<"e: "<< e; }
int main(){
int term;
cout<<"Enter a term to evaluate: ";
cin>>term;
estimate_e(term);}
Python:
def factorial(number):
if number == 0:
return 1
return number * factorial(number-1)
def estimate_e(term):
if not term:
return 0
else:
return (1 / factorial(term-1)) + estimate_e(term-1)
number = int(input("Enter how many terms to evaluate "))
print("e: ", estimate_e(number))
c)
C++
#include <iostream>
using namespace std;
int main(){
float terms, sumSeries, series;
int i, number;
cout << " Input the value of x: ";
cin >> number;
cout << " Input number of terms: ";
cin >> terms;
sumSeries = 1;
series = 1;
for (i = 1; i < terms; i++) {
series = series * number / (float)i;
sumSeries = sumSeries + series; }
cout << " The sum is : " << sumSeries << endl; }
Python
def ePowerx(number,terms):
sumSeries = 1
series =1
for x in range(1,terms):
series = series * number / x;
sumSeries = sumSeries + series;
return sumSeries
num = int(input("Enter a number: "))
term=int(input("Enter a number: "))
print("e^x: ",ePowerx(num,term))
Explanation:
a)
The program has a method factorial that takes numbers as parameter and computes the factorial of that number by using recursion. For example if the number = 3
The base case is if (number == 0)
and
recursive is return number * factorial(number - 1);
Since number = 3 is not equal to zero so the method calls itself recursively in order to return the factorial of 3
return 3* factorial(3- 1);
3 * factorial(2)
3* [2* factorial(2- 1) ]
3 * 2* [ factorial(1)]
3 * 2 * [1* factorial(1- 1) ]
3 * 2 * 1* [factorial(0)]
Now at factorial(0) the base condition is reached as number==0 So factorial(0) returns 1
Now the output is:
3 * 2 * 1* 1
return 6
So the output of this program is
Factorial of 3 is 6
b)
The method estimate_e takes a number i.e. num as parameter which represents the term and estimates the value of the mathematical constant e
The for loop iterates through each term. For example num = 3
then
e = e + 1/factorial(i);
The above statement calls works as:
e = 1 + 1/1! +1/2!
since the number of terms is 3
e is initialized to 1
i is initialized to 1
So the statement becomes:
e = 1 + 1/factorial(1)
factorial function is called which returns 1 since factorial of 1 is 1 So,
e = 1 + 1/1
e = 2
Now at next iteration at i = 2 and e = 2
e = 2 + 1/factorial(2)
e = 2 + 1/2
e = 2.5
Now at next iteration at i = 3 and e =3
e = 3 + 1/factorial(3)
e = 3 + 1/6
e = 3.16666
So the output is:
e: 3.16666
c)
The program computes the sum of series using formula:
e^x = 1 + x/1! + x^2/2! + x^3/3! +...
The for loop iterates till the number of terms. Lets say in the above formula x is 2 and number of terms is 3. So the series become:
e^x = 1 + x/1! + x^2/2!
So number = 2
terms = 3
series =1
sumSeries = 1
i = 1
The statement series = series * number / (float)i; works as following:
series = 1 * 2 /1
series = 2
sumSeries = sumSeries + series;
sumSeries = 1 + 2
sumSeries = 3
At next iteration: i=2, series =2 , sumSeries =3
series = 2 * 2/2
series = 2
sumSeries = 3 + 2
sumSeries = 5
Now the loop breaks as i=3
So output returns the value of sumSeries i.e. 5
Output:
e^x: 5