(C++) CPP Programs - 8

Friday, December 14, 2007

8. Write a C program to find the value of Sin(x) using the series

Sin(x) = x - x3/3! + x5/5! + x7/7!.......... upto N terms accuracy( without using user defined function. Also print Sin(x) using library function)

/* Sine series*/

#include

#include

#include

void main()

{

float xd,xr,acc,term,sum=0;

int i,n,option;

clrscr();

printf("1.number of terms\n2.accuracy\nEnter the option\n");

scanf("%d",&option);

if(option==1)

{

printf("Enter number of terms\n\a");

scanf("%d",&n);

printf("Enter angle in degrees\n\a");

scanf("%f",&xd);

xr=(xd*3.142)/180.0;

term=xr;

sum=xr;

for(i=1;i<=n;i++)

{

sum=-(term*xr*xr)/((2*i)*(2*i+1));

sum+=term;

}

printf("sum of sine series:sin(%f)=%f\n\a",xd,sum);

printf("Using lib function:sin(%f)=%f\n\a",xd,sin(xr));

}

else if(option==2)

{

printf("Enter the angles in degrees\n\a");

scanf("%f",&xd);

printf("Enter the acc.level[0.01-0.000001]\n\a");

scanf("%f",&acc);

xr=(xd*3.142)/180.0;

sum=xr;

term=xr;

i=1;

do

{

term=-(term*xr*xr)/((2*i)*(2*i+1));

sum+=term;

i++;

}while(fabs(term)>acc);

printf("sine series:sin(%f)=%f\n\a",xd,sum);

printf("Lib function:sin(%f)=%f\n\a",xd,sin(xr));

}

getch();

}

0 comments:

Post a Comment

Chitika

About This Blog

Followers

Blog Archive

  © Blogger template The Professional Template II by Ourblogtemplates.com 2009

Back to TOP