Swift Program to find the sine of given radian value


This tutorial will discuss how to write a Swift program to find the sine of given radian value.

A sine function is used to define the ratio of the length of the opposite side to the hypotenuse in the right-angled triangle. It is also known as the sin function. The mathematical representation of the sine() function is:

sin() = opposite Side/ hypotenuse

In Swift, we can calculate the sine of the given radian value using the pre-defined sin() function. This function return the sine value of the specified number between -1 to 1. Here, the specified number represents an angle.

Syntax

Following is the syntax −

sin(Num)

Here, the value of Num can be of integer, float, or double type.

If the given value is in degrees then we can convert degrees to radians using the following formula −

Radians = Degrees * (pi / 180)

Below is a demonstration of the same −

Input

Suppose our given input is −

Number = 1.9

Output

The desired output would be −

The value of sin 1.9 is 0.9463000876874145

Algorithm

Following is the algorithm −

  • Step 1 − Import Foundation library to use mathematic functions. import Foundation

  • Step 2 − Declare variable to store the radian value.

  • Step 3 − If the value is in degrees then, use the following formula −

Radians = Degrees * (pi / 180)

If the value is in radian ignore this step.

  • Step 4 − Find the sin value using the sin() function −

var res1 = sin(sNum1)
  • Step 5 − Print the output

Example 1

Finding sine of given radian value

The following program shows how to find the sine of the given radian value.

import Foundation import Glibc var sNum1 : Double = 1.4 var sNum2 : Double = -1.4 // Calculating the sine of the radian value // Using sin() function // For positive radian value var res1 = sin(sNum1) // For negative radian value var res2 = sin(sNum2) print("The value of sin \(sNum1) is ", res1) print("The value of sin \(sNum2) is ", res2)

Output

The value of sin 1.4 is  0.9854497299884601
The value of sin -1.4 is  -0.9854497299884601

Here, in the above code, we find the sine value of the given radian using the sin() function −

var res1 = sin(sNum1)
var res2 = sin(sNum2)

Display the result sin 1.4 is 0.9854497299884601 and sin -1.4 is -0.9854497299884601

Example 2

Finding sine of given degrees value

The following program shows how to find the sine value of the given degrees.

import Foundation import Glibc var sNum1 = 90.0 var sNum2 = 45.0 // Convert degrees into radian var radian1 = sNum1 * (Double.pi / 180) var radian2 = sNum2 * (Double.pi / 180) // Calculating the sine value // Using sin() function var res1 = sin(radian1) var res2 = sin(radian2) print("The value of sin \(sNum1) degrees is ", res1) print("The value of sin \(sNum2) degrees is ", res2)

Output

The value of sin 90.0 degrees is  1.0
The value of sin 45.0 degrees is  0.7071067811865475

Here, in the above code, we calculate the value of sin of the given degrees. Here, first we convert the degrees into radian using the following code:

var radian1 = sNum1 * (Double.pi / 180)

And then calculate the sine value using the sin() function −

var res1 = sin(radian1)

Display the result: sin 90.0 degrees is 1.0 and sin 45.0 degrees is 0.7071067811865475.

Updated on: 25-Aug-2022

581 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements