Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
<div class="execute"></div><div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation <span class="token keyword">import</span> Glibc <span class="token keyword">var</span> sNum1 <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">1.4</span> <span class="token keyword">var</span> sNum2 <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1.4</span> <span class="token comment">// Calculating the sine of the radian value</span> <span class="token comment">// Using sin() function</span> <span class="token comment">// For positive radian value</span> <span class="token keyword">var</span> res1 <span class="token operator">=</span> <span class="token function">sin</span><span class="token punctuation">(</span>sNum1<span class="token punctuation">)</span> <span class="token comment">// For negative radian value</span> <span class="token keyword">var</span> res2 <span class="token operator">=</span> <span class="token function">sin</span><span class="token punctuation">(</span>sNum2<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"The value of sin \(sNum1) is "</span><span class="token punctuation">,</span> res1<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"The value of sin \(sNum2) is "</span><span class="token punctuation">,</span> res2<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
<div class="execute"></div><div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation <span class="token keyword">import</span> Glibc <span class="token keyword">var</span> sNum1 <span class="token operator">=</span> <span class="token number">90.0</span> <span class="token keyword">var</span> sNum2 <span class="token operator">=</span> <span class="token number">45.0</span> <span class="token comment">// Convert degrees into radian</span> <span class="token keyword">var</span> radian1 <span class="token operator">=</span> sNum1 <span class="token operator">*</span> <span class="token punctuation">(</span>Double<span class="token punctuation">.</span>pi <span class="token operator">/</span> <span class="token number">180</span><span class="token punctuation">)</span> <span class="token keyword">var</span> radian2 <span class="token operator">=</span> sNum2 <span class="token operator">*</span> <span class="token punctuation">(</span>Double<span class="token punctuation">.</span>pi <span class="token operator">/</span> <span class="token number">180</span><span class="token punctuation">)</span> <span class="token comment">// Calculating the sine value</span> <span class="token comment">// Using sin() function</span> <span class="token keyword">var</span> res1 <span class="token operator">=</span> <span class="token function">sin</span><span class="token punctuation">(</span>radian1<span class="token punctuation">)</span> <span class="token keyword">var</span> res2 <span class="token operator">=</span> <span class="token function">sin</span><span class="token punctuation">(</span>radian2<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"The value of sin \(sNum1) degrees is "</span><span class="token punctuation">,</span> res1<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"The value of sin \(sNum2) degrees is "</span><span class="token punctuation">,</span> res2<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
