-
Help!!
i've just started learning C language from two weeks ago (and its the first programming language that i learn!!)
i have to do the following assignment:
Write a program that computes the value of e^x by using the formula:
e^x = 1 + x/1! + x^2 / 2! + x^3 / 3! +.......
i wrote the following program but i keep receiving error!
#include <stdio.h>
#include <math.h>
int main ()
{
float x, result,fact=1; int power=0,a=1;
printf ("Enter the value of X");
scanf ("%f",&x);
result= 0;
fact=power;
while (power <= 3){
++power;
while ( (power-a) >= 1){
fact= fact * (power-a);
++a;
}
result = (result + pow(x,power)) / fact;
if (power >= 3) {
printf ("the result is %f",result + 1);
break;
}
}
return 0;
}
thanx for any help and sorry for my bad english.
-
> i wrote the following program but i keep receiving error!
You know, well obviously you don't, it is often USEFUL to actually have the error message. I'm going going to read all of your code and sit around and figure out what error you're getting. If you want help, be helpful and provide us with the information we need to be of assistance.
Quzah.
-
Seems to work OK for me (under Dev C++)
-
I compiled and ran it through gcc, and while it compiles the output is always the same.
-
its me again!
i receive the following error:
The exception Floating-point division by zero.
(0xc0000008e) occured in the application at location 0x004011d6
i am using Borland C++ Builder 5.5
thanx for trying to help me.
-
i changed power=0 to power=1 and now it works fine, but the output is wrong!!!! :(
please HELP!
-
>result= 0;
>fact=power;
Now fact == 0.
>while (power <= 3){
>++power;
>while ( (power-a) >= 1){
>fact= fact * (power-a);
>++a;
>}
Here fact == 0, since 0 * A = 0, whatever the value of A is.
>result = (result + pow(x,power)) / fact;
This all results in a division by 0.