C Programming

# C: Sincos Functions Usage

The “sin()” and “cos()” functions are used very commonly while dealing with angles in mathematics. Similarly, while doing mathematical computations in programming that revolve around geometry or trigonometry, these functions prove very useful. The C programming language also implements these functions in its “math.h” header file. After including this header file in the C code, the programmers can conveniently use these functions to calculate the trigonometric ratios. In this guide, we will try to explore the usage of the “sin()” and “cos()” functions of the C programming language.

## Usage of the Sin and Cos Functions in C:

The usage of the sine and cosine functions of the C programming language has been depicted through the following five examples:

## Example # 1: Finding the Sine and Cosine of a Positive Angle in Radians:

For finding the sine and cosine of a positive angle in radians in the C programming language, you will have to implement the code shown below:

In this program, we have included the “math.h” header file that contains the implementation of the “sin()” and “cos()” functions along with the “stdio.h” header file. Then, we defined a float variable “x” and assigned a positive angle “90” to it. After that, we have defined two other float variables for holding the results of the “sin()” and “cos()” functions, respectively. Finally, we have used the “printf” statements for displaying the values of the “sin()” and “cos()” functions on the terminal, followed by the “return 0” statement.

For compiling this script, we have used the following command:

\$ gcc sincos.c –o sincos –lm

We have used the “-lm” option in this command to link the “math.h” header file with the compiled program. Without doing this, you will not be able to compile this script.

Then, for executing this script, we have used the command shown below:

\$ ./sincos

The sine and cosine results of the provided positive angle in radians are shown in the following image:

## Example # 2: Finding the Sine and Cosine of a Negative Angle in Radians:

For finding the sine and cosine of a negative angle in radians in the C programming language, you will have to implement the code shown below:

In this program, we have included the “math.h” header file that contains the implementation of the “sin()” and “cos()” functions along with the “stdio.h” header file. Then, we defined a float variable “x” and assigned a negative angle “-90” to it. After that, we have defined two other float variables for holding the results of the “sin()” and “cos()” functions, respectively. Finally, we have used the “printf” statements for displaying the values of the “sin()” and “cos()” functions on the terminal, followed by the “return 0” statement.

The sine and cosine results of the provided negative angle in radians are shown in the following image:

## Example # 3: Finding the Sine and Cosine of “0” Angle in Radians:

For finding the sine and cosine of the “0” angle in radians in the C programming language, you will have to implement the code shown below:

In this program, we have included the “math.h” header file that contains the implementation of the “sin()” and “cos()” functions along with the “stdio.h” header file. Then, we defined a float variable “x” and assigned the angle “0” to it. After that, we have defined two other float variables for holding the results of the “sin()” and “cos()” functions, respectively. Finally, we have used the “printf” statements for displaying the values of the “sin()” and “cos()” functions on the terminal, followed by the “return 0” statement.

The sine and cosine results of the angle “0” in radians are shown in the following image:

## Example # 4: Finding the Sine and Cosine of the Given Angle in Degrees:

For finding the sine and cosine of the given angle in degrees in the C programming language, you will have to implement the code shown below:

In this program, we have included the “math.h” header file that contains the implementation of the “sin()” and “cos()” functions along with the “stdio.h” header file. Moreover, we have also defined the “PI” variable. Then, we defined a float variable “x” and assigned a positive angle “90” to it. After that, we defined a float variable “degrees” and assigned the value “PI/180” to it. Then, we have defined two other float variables for holding the results of the “sin()” and “cos()” functions, respectively, but this time, the results are also multiplied with the “degree” variable. Finally, we have used the “printf” statements for displaying the values of the “sin()” and “cos()” functions on the terminal, followed by the “return 0” statement.

The sine and cosine results of the provided angle in degrees are shown in the following image:

## Example # 5: Finding the Sine and Cosine of an Angle in Degrees Provided at Runtime:

For finding the sine and cosine of an angle in degrees provided at the runtime in the C programming language, you will have to implement the code shown below:

In this program, we have included the “math.h” header file that contains the implementation of the “sin()” and “cos()” functions along with the “stdio.h” header file. Moreover, we have also defined the “PI” variable. Then, we have defined a float variable “x” and have asked the user to enter its value by displaying a message on the terminal. After that, we defined a float variable “degrees” and assigned the value “PI/180” to it. Then, we have defined two other float variables for holding the results of the “sin()” and “cos()” functions, respectively, but this time, the results are also multiplied with the “degree” variable. Finally, we have used the “printf” statements for displaying the values of the “sin()” and “cos()” functions on the terminal, followed by the “return 0” statement.

Upon the execution of this code, the user was asked to enter an angle of his choice, as shown in the image below:

The sine and cosine results of the angle provided by the user at the runtime in degrees are shown in the following image:

## Conclusion:

With the help of this guide, we aimed to teach you the usage of the “sin()” and “cos()” functions of the C programming language. For that, we shared with you five different examples in which we have used these functions. We calculated these values for positive, negative, and zero angles, respectively. Moreover, we also taught you the procedure of calculating these values in degrees instead of radians since these functions return the results in radians by default in the C programming language. We also explained how you can take the angle whose sine and cosine are found from the user at the runtime. After going through these different variations, you can confidently use the “sin()” and “cos()” functions within the C programming language in Linux.