write a 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.
int x,n,counter;
printf("Enter value of x : ");
scanf ("%d",&x);
printf("Enter number of terms (n) : ");
scanf ("%d",&n);
counter=counter+1;
this is all I have :/ I'm not sure how to start my loops on this one.
in class all we've covered up to is for loops, while loops, and inner loops, if else loops as well. I know the value of e=2.7182. I just can't figure out what should go inside the loops.
int x,n, factorial=1;
printf("Enter value of x : ");
scanf ("%d",&x);
printf("Enter number of terms (n) : ");
scanf ("%d",&n);
for(x=1; x<=n, x++)
{
fact = fact * x
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
okay, so I'm pretty sure the for loop is wrong..ahhhh! is it normal for programming to be this hard? this is my first time taking it. I mean i understand what each loop does, and whats going on, but writing it is a different story.
Programming is generally easier for people who think in a very logical manner. It will obviously be harder for other people, but as with anything else, practice makes it easier.
The problem with your code is that you're missing a closing brace after the loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <stdio.h>
#include <math.h>
int main()
{
int x,n, factorial=1;
printf("Enter value of x : ");
scanf ("%d",&x);
printf("Enter number of terms (n) : ");
scanf ("%d",&n);
for(x=1; x<=n, x++)
{
fact = fact * x
// } <--missing this one
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
You might also find it easier to use C++ streams rather than the C I/O functions, but that's something you need to decide about whether you're ready - just don't mix them.
You could follow the suggestion from Peter7, to use a function to calculate the factorial, and the library function pow() and find each term of the series like that. In fact the code would probably look quite clean and tidy that way.
But there is another approach. Since we are dealing with a series, and need to find every term, we can take advantage of the fact that there is a simple relationship of each successive term to the previous one. That means each term requires just one multiply and one divide. Considerably simpler and more efficient.
Lets look again at the series: e^x = 1 + x/1! + x^2/2! + x^3/3!...
first term = 1
second term = (first term) * x / 1
third term = (second term) * x / 2
fourth term = (third term) * x / 3
...
Now all you need to do is create a loop which calculates each term as described, and adds them to a variable which will accumulate the total.
In fact, it is usually specified in the instructions for this sort of exercise that you are not allowed to use the functions in the <cmath> library. After all, if it is acceptable to use pow(), then why not just use exp() to get the answer?
thats the thing, i dont know what to put in my loops. Im honestly lost on this problem like dead lost. i feel dumb cause I know once i see hte code, imma feel dumb for not figuring it out.
#include <iostream.h>
double factorial (double num) // this is the function that i have created for factorial
{
double fact; //makes it easier for me to convert this equation into C++ program.
fact=1.00;
while(num>0)
{
fact=fact*num;
num=num-1;
}
return (fact);
}
double powucp (double a, double b) // This is another function that i have created to calculate power
// since we are also using power function to convert this equationt to a C++ Program.
{
double power;
power=a;
double n;
n=1;
while(n<b)
{
power=power*a;
n=n+1;
}
return power;
}
void main() // our actual program starts here...
{
double ex,x;
double power;
double n;
cout<<"Please enter the value of x: ";
cin>>x;
cout<<"Enter the number of terms that u want to calculate this formula to(number of terms=power): ";
cin>>power;
n=1;
ex=1;
while(n<=power)
{
ex=ex+(powucp(x,n)/factorial(n)); // all done using the functions that i have created above
n=n+1; //makes things a lot easier using the functions
}
cout<<"Result is: "<<ex<<'\n';
}
You need to calculate the sum of the terms of a series.
Here's an example which simply gets the sum of the integers from 1 to 10.
1+2+3+4+5+6+7+8+9+10
It uses a loop to do so.
The principle is very common in many programs. Before the loop starts, do whatever initialisation is required.
in this case that means define the total and set it to zero.
Inside the loop, add the value of each term of the series to the total.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main()
{
// calculate sum of integers from 1 to n
int n = 10;
double total = 0;
for (int i=1; i<=n; i++)
{
total = total + i;
}
cout << "Sum of integers from 1 to " << n << " is " << total << endl;
return 0;
}
You should be able to adapt this idea to find the sum of any series.
This is as near as I can get without solving the entire problem for you.
The main thing you need to do is to replace the repeated code with a proper loop where the number of terms accumulated will agree the the number requested by the user.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double x;
int n;
cout << "Enter value of x : ";
cin >> x;
cout << "Enter number of terms (n) : ";
cin >> n;
// Initialisation
double term = 1.0; // First term in the series
double total = 0.0; // This is the sum of the series
int count = 0; // Number of terms so far
// Accumulate total of each term in the series.
total = total + term; // result after accumulating one term
count = count + 1;
term = term * x / count; // generate the next term
total = total + term; // result after accumulating two terms
count = count + 1;
term = term * x / count; // generate the next term
total = total + term;
count = count + 1;
term = term * x / count;
//---- repeat the above as many times as required
cout << "e to the power " << x << " is " << setprecision(10) << total << endl;
return 0;
}
Note, if you use a for-loop, the code will get shorter, as both line 18 and this line count = count + 1;
are done in the for statement, which means the code ends up remarkably concise.