a)
C++
#include<iostream>
using namespace std;
int factorial(int num) {
if (num == 0)
return 1;
return num * factorial(num - 1); }
int main() {
int integer;
cout<<"Enter a non negative integer: ";
cin>>integer;
cout<< "Factorial of "<< integer<<" is "<< factorial(integer)<< endl; }
Python:
def factorial(num):
if num == 0:
return 1
return num * factorial(num-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 includes a factorial method that takes a number as an argument and calculates its factorial using recursion. For instance, if number = 3
The base case occurs at if (number == 0)
and the recursion is handled with return number * factorial(number - 1);
With number = 3 not equaling zero, the function calls itself recursively to get 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)]
At this point at factorial(0), the base condition is satisfied as number==0, so factorial(0) returns 1
The resulting output is:
3 * 2 * 1* 1
yielding 6
So, the final program output will be
Factorial of 3 is 6
b)
The estimate_e method takes a number, termed as num, which signifies the term to estimate the mathematical constant e
The for loop extends through each term. For example, if num is set to 3
Then the core statement:
e = e + 1/factorial(i);
The preceding calculation works as:
e = 1 + 1/1! +1/2!
Since the term count is 3
Initially, e is set to 1
i is initialized at 1
Inserting this into the calculation gives us:
e = 1 + 1/factorial(1)
The factorial function computes and returns 1, as the factorial of 1 is 1. Thus,
e = 1 + 1/1
This results in e = 2
Proceeding to the next iteration, where i = 2 and e = 2, we calculate e = 2 + 1/factorial(2)
Thus, e = 2 + 1/2 results in e = 2.5
Following to the next iteration with i = 3, we have e = 3 + 1/factorial(3)
This yields e = 3 + 1/6 resulting in approximately e = 3.16666
Therefore, the output is:
e: 3.16666c)
This program calculates the sum of a series based on the formula:
e^x = 1 + x/1! + x^2/2! + x^3/3! +...
The for loop iterates according to the number set for terms. Assuming x is 2, and the number of terms is set to 3, the series would read:
e^2 = 1 + 2/1! + 2^2/2!
In this setup: number = 2 and terms = 3
Initial values for series and sumSeries are both 1
Starting with i equal to 1, the update statement series = series * number / (float)i; applies as follows:series = 1 * 2 /1 results in series = 2
Then, for sumSeries, we have sumSeries = sumSeries + series; Outputs sumSeries as 1 + 2, yielding 3
Continuing to the next iteration: i=2, with series = 2 and sumSeries = 3, we recalculate as series = 2 * 2/2 imposing series = 2 again. Thus, we find: sumSeries = 3 + 2 giving a final sumSeries value of 5
After the loop concludes, the result shows the value of sumSeries, leading finally to the output value of 5