Home >Backend Development >C++ >C program written using math.h library to calculate cosine and sine values

C program written using math.h library to calculate cosine and sine values

WBOY
WBOYforward
2023-09-06 09:49:06966browse

C program written using math.h library to calculate cosine and sine values

Question

Find the cosine and sine values ​​for every 10 degrees between 0 and 150.

Solution

The logic used to find Cosine valueis as follows-

Declare the MAX and PI values ​​at the beginning of the program

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

The logic used to find the sine value is as follows -

Declare the MAX and PI values ​​at the beginning of the program.

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

Example

The following is a C program to find the cosine value-

//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)</p><p></p><p>");
   while(angle <= MAX) {
      x = (PI/MAX)*angle;
      y = cos(x);
      printf("%15d %13.4f</p><p>", angle, y);
      angle = angle + 10;
   }
}

Output

When executing the above program, the following output will be produced-

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

The following is a C program to find the sine value-

//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)</p><p></p><p>");
   while(angle <= MAX){
      x = (PI/MAX)*angle;
      y = sin(x);
      printf("%15d %13.4f</p><p>", angle, y);
      angle = angle + 10;
   }
}

Output

When executing the above program, the following output is produced -

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

The above is the detailed content of C program written using math.h library to calculate cosine and sine values. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete