Write a C program to evaluate the value of e x by using the formula: e x = 1 + x
ID: 675547 • Letter: W
Question
Write a C program to evaluate the value of e x by using the formula:
e x = 1 + x / 1! + x 2 / 2! + x 3 / 3! + . . . . . .
Evaluate the above expression for different values of x and number of terms in the expression. The user should be allowed enter x and number of terms for which the expression is calculated.
here is my code:
#include <stdio.h>
int power(int base, int exp)
{
int total = 1;
while (exp >= 1)
{
total *= base;
exp--;
}
return total;
}
int factorial(int n)
{
int counter = 1;
while (n >= 1)
{
counter *= n;
n--;
}
return counter;
}
int main()
{
int e, x;
double total = 1;
printf("Enter two numbers (base, exponent): ");
scanf("%d%d", &e, &x);
while (x >= 1)
{
total += (double)power(e, x) / factorial(x);
x--;
}
printf("The result = %.2f ", total);
return 0;
}
Is this correct? the way i structured. i am not sure if i am getting the value of e when i input my numbers please correct me this is in C
Explanation / Answer
you have to power of (x,x) as follows
#include <stdio.h>
int power(int base, int exp)
{
int total = 1;
while (exp >= 1)
{
total *= base;
exp--;
}
return total;
}
int factorial(int n)
{
int counter = 1;
while (n >= 1)
{
counter *= n;
n--;
}
return counter;
}
int main()
{
int e, x;
double total = 1;
printf("Enter two numbers (base, exponent): ");
scanf("%d%d", &e, &x);
while (x >= 1)
{
total += (double)power(x, x) / factorial(x);
x--;
}
printf("The result = %.2f ", total);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.