C Programming

Sin function in C

In mathematics, the sine function is among three primary functions, others comprising cosine and tan. Techniques for executing basic mathematical operations, like the elementary exponential, logarithmic, square root, and mathematical operations, are included in the <math.h> header. You must include the header file <math.h> to utilize these functions. The sin function accepts an angle in radians and gives its sine value, which can be confirmed using a sine curve.

You may use the law of sine to find any arbitrary angle in a triangle, as well as the length of a certain triangle side. This is a fundamental trigonometric notion. The sin function is used in the ANSI/ISO 9899-1990 versions of the C language. Sin () returns a result that is between 1 and -1.

Now, let us start with a few examples of sin() function in the C programming language.

Pre-requisites

  • Installation of GCC Compiler on Windows/ Linux

Note: We are using the GCC compiler in Windows 10.

Example 1

In our first illustration, we are going to use the sin() function in our program. Initially, you have to open a GCC compiler and create a new file with any required name. After that, insert the subsequent code into it.

We have added a few libraries that are < stdio.h > and < math.h >. Once added, move towards the main function. In the main() function, we have declared two variables, “a” and “result” with the double data type. To one of the variables, we have assigned it a value of “7.1” and calculated its sin value; the output will be stored in the “result” variable. Then we have assigned a negative value to variable “a” and calculated its sin value. After that, zero has been assigned to variable “a” and its calculating result will be stored in the “result” variable. Now we are all set to execute the program. In the menu bar of the GCC compiler, you have to click on the “Compile and Run” option to get the output. This option is present under the “Build” menu.

Once you click on this option, a black screen titled “Console” will appear on your screen. You will get the following affixed output as presented in the below-attached screenshot.

Example 2

In our second example, we will use the sin() function in our program in a little complex way. Initially, you have to open a GCC compiler and create a new file with any required name. The previous file can also be used. After that, insert the subsequent code into it.

We have added a few libraries that are < stdio.h > and < math.h >. We have added the value of PI, as can be seen in the screenshot. Once added, move towards the main function. In the main() function, we have declared three variables “a”, “retu”, and “value” with the double data type. To one of the variables, we have assigned it a value of “79.0”. We have applied a formula, saved its value in the “retu” variable, and calculated its sin value; then, we have printed the result using the printf() function. Now we are all set to execute the program. In the menu bar of the GCC compiler, you have to click on the “Compile and Run” option to get the output. This option is present under the “Build” menu.

Once you click on this option, a black screen titled “Console” will appear on your screen. You will acquire the following affixed output as presented in the below-attached screenshot.

Example 3

In our third and last example, we will use the sin() function in our program in a little different way. That user has to enter a number at run time to calculate the sin() value. Initially, you have to open a GCC compiler and create a new file with any required name. The previous file can also be used. After that, insert the subsequent code into it.

We have added a few libraries that are  < stdio.h > and  < math.h >. Once added, move towards the main function. In the main() function, we have declared two variables, “Sineval” and “num” with the double data type. We have used the printf() and scanf() function. After that, we will calculate the sin() of the value entered by the user and display its result.

Now we are prepared to execute the program. In the menu bar of the GCC compiler, you have to click on the “Compile and Run” option to get the output. This option is present under the “Build” menu. Once you click on this option, a black screen titled “Console” will appear on your screen. You will get the following affixed output as shown in the below-attached screenshot

You can see that we have entered the “19” number and got its result as well. The number can be chosen as per your need.

Conclusion

This article has provided a brief introduction regarding the sin() function in the C programming language. We have elaborated on three different examples for user understanding. All examples are different in implementation. Now, I hope whenever you try to implement these examples on your system, you can easily understand the basic concept of sin() function in the C programming language.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content