Thursday, November 27, 2014

C Program to calculate sin or cos of an angle in degrees

/*................................................................................................................................................
File :rec14.c
Program :Menu driven function sub program for finding the
1. cosine of an angle in degrees
2.sine of an angle in degrees
Author : 141740
Date : 15-11-14
................................................................................................................................................. */
#include<stdio.h>
#include<math.h>
float CosX(float theta);
float SinX(float theta);
main()
{
int choice;
float cosx,sinx,th;
system("clear");
printf("Program to claulate sin or cos of an angle in degrees\n\n");
printf("Enter your choice\n");
printf("\t1: Cos of angel in degrees\n");
printf("\t2: Sin of angle in degrees\n");
scanf("%d",&choice);
printf("Enter the value of theta\n");
scanf("%f",&th);
if(choice==1){
cosx=CosX(th);
printf("Cos of %f = %f",th,cosx);
}else if(choice==2){
sinx=SinX(th);
printf("Sin of %f = %f",th,sinx);
}else{
printf("Enter proper choice\n");
}
}
float CosX(float theta)
{
int i;
float cosx,x,th;
x=(theta*3.14)/180.0;
th=1;
cosx=1;
i=1;
while(fabs(th)>0.0001){
th=(-1)*(th*x*x)/(((2*i)-1)*(2*i));
cosx=cosx+th;
i++;
}
return cosx;
}
float SinX(float theta)
{
int i;
float sinx,x,th;
x=(theta*3.14)/180.0;
th=x;
sinx=x;
i=1;
while(fabs(th)>0.0001){
th=(-1)*(th*x*x)/(((2*i)+1)*(2*i));
sinx=sinx+th;
i++;
}
return sinx;
}

OUTPUT 1
Program to calculate sin or cos of an angle in degrees
Enter your choice
1: Cos of angel in degrees
2: Sin of angle in degrees
1
Enter the value of theta
56
Cos of 56.000000 = 0.559604[141740@localhost ~]$

OUTPUT 2
Program to calculate sin or cos of an angle in degrees
Enter your choice
1: Cos of angel in degrees
2: Sin of angle in degrees
2
Enter the value of theta
60
Sin of 60.000000 = 0.865760[141740@localhost ~]$

No comments:

Post a Comment