Computer >> Computer tutorials >  >> Programming >> C programming

C program to find out cosine and sine values using math.h library.


Problem

To find the cosine and sine values for every 10 degrees from 0 to 150.

Solution

The logic used to find the cosine values is as follows −

Declare MAX and PI value at the starting of a program

while(angle <= MAX){
   x = (PI/MAX)*angle;
   y = cos(x);
   printf("%15d %13.4f\n", angle, y);
   angle = angle + 10;
}

The logic used to find the sine values is as follows −

Declare MAX and PI value at the starting of a program.

while(angle <= MAX){
   x = (PI/MAX)*angle;
   y = sin(x);
   printf("%15d %13.4f\n", angle, y);
   angle = angle + 10;
}

Example

Following is the C program to find the cosine values −

//cosine values
#include<stdio.h>
#include <math.h>
#define PI 3.1416
#define MAX 150
main ( ) {
   int angle;
   float x,y;
   angle = 0;
   printf("Angle cos(angle)\n\n");
   while(angle <= MAX) {
      x = (PI/MAX)*angle;
      y = cos(x);
      printf("%15d %13.4f\n", angle, y);
      angle = angle + 10;
   }
}

Output

When the above program is executed, it produces the following output −

Angle cos(angle)
0 1.0000
10 0.9781
20 0.9135
30 0.8090
40 0.6691
50 0.5000
60 0.3090
70 0.1045
80 -0.1045
90 -0.3090
100 -0.5000
110 -0.6691
120 -0.8090
130 -0.9135
140 -0.9781
150 -1.0000

Example

Following is the C program to find the sine values −

//sine values
#include<stdio.h>
#include <math.h>
#define PI 3.1416
#define MAX 150
main ( ){
   int angle;
   float x,y;
   angle = 0;
   printf("Angle sin(angle)\n\n");
   while(angle <= MAX){
      x = (PI/MAX)*angle;
      y = sin(x);
      printf("%15d %13.4f\n", angle, y);
      angle = angle + 10;
   }
}

Output

When the above program is executed, it produces the following output −

Angle sin(angle)

0 0.0000
10 0.2079
20 0.4067
30 0.5878
40 0.7431
50 0.8660
60 0.9511
70 0.9945
80 0.9945
90 0.9511
100 0.8660
110 0.7431
120 0.5878
130 0.4067
140 0.2079
150 -0.0000