Skip to main content

To evaluate the value of e^-x upto a predefined accuracy

/* To evaluate the value of e^-x */
#include<stdio.h>
#include<math.h>
int main()
{
    float  sum, t;
    int i, x;
    printf("Value of x:\n");
    scanf("%d", &x);
    sum=1.0;
    t=1;
    i=0;
    label:
        i=i+1;
        t=-t*x/i;
/*Setting a predefined accuracy=0.00001*/
        if(fabs(t)>0.00001)
    {
        sum=sum+t;
        goto label;
    }
    else
    printf("e^-%d=%f\n", x, sum);
    return(0);
}

Comments