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
Release: 2023-09-06 09:49:06
forward
964 people have browsed it

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;
}
Copy after login

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;
}
Copy after login

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;
   }
}
Copy after login

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
Copy after login

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;
   }
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template