Lab Record Program No 8
8.Write a C Program to find Sin(x) and Cos(x) values using series expansion.
A)PROGRAM FOR SUM OF SIN (X) SERIES /*x -x^3/3! + x^5/5! - .........*/
#include<stdio.h>
#include<math.h>
void main()
{
float x,s=0;
int fact(int x);
int n,i,t=1;
clrscr();
printf("\nEnter the value of x & n ");
scanf("%f%d",&x,&n);
for(i=1; i<=n; i++)
{
s=s+pow(-1,(i+1))*(pow(x,t))/((float)fact(t));
printf("x^%d/%d!",t,t);
if(i%2==0)
printf("+");
else
printf("-"); t=t+2;
}
printf("\nSum of series = %.2f",s);
getch();
}
int fact(int x)
{
int i,f=1;
for(i=1; i<=x; i++)
f=f*i;
return(f);
}
B)PROGRAM FOR SUM OF COS (X) SERIES
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float x, sum=1, t=1;
clrscr();
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
/* Loop to calculate the value of Cosine */
for(i=1;i<=n;i++)
{
t=t*(-1)*x*x/(2*i*(2*i-1));
sum=sum+t;
}
printf(" The value of Cos(%f) is : %.4f", x, sum);
getch();
}
Comments
Post a Comment