Friday, July 20, 2012

C Program to find e^x


/* To find e^x */

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define f0(x) pow(2.34176,x)

#define f1(x) pow(2.34176,x)

#define f2(x) pow(2.34176,x)

#define f3(x) pow(2.34176,x)

#define f4(x) pow(2.34176,x)

void main()

{

float t,x,h,v,i=0,ea=100;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("Enter value of x & h to find ln(x)");

scanf("%f %f",&x,&h);

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("------------------------------------------------");

puts("Iter. ln(x+h) \tEa");

puts("------------------------------------------------");

v=f0(x);

printf("%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*f1(x);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*f2(x)/2;

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*h*f3(x)/(3*2);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*h*h*f4(x)/(4*3*2);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

puts("\n------------------------------------------------");

getch();

}

Output of Program:-




First:-




Enter value of x & h to find e^(x+h)

2 1

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

------------------------------------------------

Iter. e^(x+h) Ea

------------------------------------------------

0 5.483840 100.000000

1 10.967680 50.000000

2 13.709599 19.999998

3 14.623572 6.249997

4 14.852066 1.538464

------------------------------------------------







Second:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Enter value of x & h to find e^(x+h)

5 0.5

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

------------------------------------------------

Iter. e^(x+h) Ea

------------------------------------------------

0 70.422577 100.000000

1 105.633865 33.333332

2 114.436691 7.692311

3 115.903831 1.265826

4 116.087227 0.157981

------------------------------------------------

















No comments:

Post a Comment