- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to find the cosine of given radian value
This tutorial will discuss how to write a Swift program to find the cosine of given radian value.
A cosine function is used to define the ratio of the length of the adjacent side to the hypotenuse in the right-angled triangle. It is also known as the cos function. The mathematical representation of the cosine() function is −
cos() = adjacent Side/ hypotenuse
In Swift, we can calculate the cosine of the given radian value using the pre-defined cos() function. This function return the cosine value of the specified number between -1 to 1. Here, the specified number represent an angle.
Syntax
Following is the syntax −
cos(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.2
Output
The desired output would be −
The value of cos 1.2 is 0.3623577544766736
Algorithm
Following is the algorithm −
Step 1 − Import the Foundation library to use mathematic functions. import Foundation
Step 2 − Declare variable to store radian value.
Step 3 − If the value is in degrees then use the following formula −
If the value is in radian ignore this step.
Step 4 − Find the cosine value using the cos() function −
var res1 = cos(cNum1)
Step 5 − Print the output.
Example 1
Finding cosine of given radian value
The following program shows how to find the cosine of the given radian value.
import Foundation import Glibc var cNum1 : Double = 1.3 var cNum2 : Double = -1.3 // Calculating the cosine of the radian value // Using cos() function // For positive radian value var res1 = cos(cNum1) // For negative radian value var res2 = cos(cNum2) print("The value of cos \(cNum1) is ", res1) print("The value of cos \(cNum2) is ", res2)
Output
The value of cos 1.3 is 0.26749882862458735 The value of cos -1.3 is 0.26749882862458735
Here, in the above code, we find the cosine value of the given radian using the cos() function −
var res1 = cos(cNum1) var res2 = cos(cNum2)
Display the result: cos 1.3 is 0.26749882862458735 and cos 1.3 is 0.26749882862458735.
Example 2
Finding cosine of given degrees value
The following program shows how to find the cosine value of the given degrees.
import Foundation import Glibc var cNum1 = 45.0 var cNum2 = 30.0 // Convert degrees into radian var radian1 = cNum1 * (Double.pi / 180) var radian2 = cNum2 * (Double.pi / 180) // Calculating the cosine value // Using cos() function var res1 = cos(radian1) var res2 = cos(radian2) print("The value of cos \(cNum1) degrees is ",res1) print("The value of cos \(cNum2) degrees is ",res2)
Output
The value of cos 45.0 degrees is 0.7071067811865476 The value of cos 30.0 degrees is 0.8660254037844387
Here, in the above code, we calculate the value o f cosine of the given degrees. Here, first we convert the degrees into radian using the following code:
var radian1 = cNum1 * (Double.pi / 180)
And then calculate the cosine value using the cos() function −
var res1 = cos(radian1)
Display the result: cos 4 5.0 degrees is 0.7071067811865476 and cos 30.0 degrees is 0.8660254037844387