(C++) CPP Programs - 9

Friday, December 14, 2007

9. Write a C program to find the value of Cos(x) using the series

Cos(x) = 1 - x2/2! + x4/4! + x6/6!.......... upto N terms accuracy( without using user defined function. Also print Cos(x) using library function)

/*Cosine 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=1;

sum=1;

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

{

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

sum+=term;

}

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

printf("Using lib function:cos(%f)=%f\n\a",xd,cos(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=1;

term=1;

i=1;

do

{

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

sum+=term;

i++;

}while(fabs(term)>acc);

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

printf("Lib function:cos(%f)=%f\n\a",xd,cos(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