- Trending Categories
- 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 GCD of two Numbers

This tutorial will discuss how to write a Swift program to find GCD of two numbers.

GCD is also known as Greatest Common Divisor or HCF(Highest common factor). GCD of two positive number is defined as the greater positive number which is the common factor of both the given two positive numbers. The value of GCD cannot be 0 or negative. The minimum value of GCD of two numbers is always 1. For example, we have two numbers 24 and 30.

Factor of 24 = 2 x 2 x 2 x 3

Factor of 30 = 2 x 3 x 5

So the GCD(24, 30) = 2 x 3 = 6 because 2 x 3 are common factors.

Below is a demonstration of the same −

**Input**

Suppose our given input is

Number 1 = 24 Number 2 = 30

**Output**

The desired output would be

GCD = 6

**Euklid's algorithm**

Generally to find the GCD of two numbers we need to first find their factors and then take the greatest common number they have to calculate GCD. This method is effective only for small numbers but if the numbers are largest this method is quit difficult to find GCD. So to over come from this difficulty we use Euklid's algorithm.

In this Euklid's algorithm, we divide two numbers till the value of their remainder is 0.

GCD(m, n) = GCD(n, m%n)

Here, m%n is use to find remainder of m and n.

In Swift we can GCD of two numbers using any of the following methods

## Method 1 - Iterative Method

We can calculate GCD using the while loop.

### Algorithm

Following is the algorithm −

**Step 1**− Create a function with two arguments.**Step 2**− Declare a variable with value 0 that is x = 0.**Step 3**− Declare two variable named y and z of int type. Here, y stores the maximum number among num1 and num2 using max() function whereas z stores the minimum number among num1 and num2 using min() function.

var y: Int = max(num1, num2) var z: Int = min(num1, num2)

Here these max() and min() makes sure that the largest number should divides by the smallest number.

**Step 4**− Rum while loop till z != 0.**Step 5**− Find the remainder using % operator.**Step 6**− Return GCD**Step 7**− Call findGCD(num1:78, num2:46) function with two argument and store the output in result variable.**Step 8**− Print output.

### Example

The following program shows how to find GCD of two numbers using iterative approach.

import Swift import Foundation // Function to find gcd of two numbers func findGCD(num1: Int, num2: Int) -> Int { var x = 0 // Finding maximum number var y: Int = max(num1, num2) // Finding minimum number var z: Int = min(num1, num2) while z != 0 { x = y y = z z = x % y } return y } // Calling Function var result = findGCD(num1:78, num2:46) print("GCD of 78 and 46 is ", result)

### Output

GCD of 78 and 46 is 2

Here, in the above code, we create a function named findGCD() to find the GCD of two numbers. This function follows Euklid's algorithm. So first find the greatest and smallest number among the given two numbers so that the largest number should divides by the smallest number. Then find their reminder till the value of reminder is 0. Now call the findGCD() function and pass 78 and 46 as an arguments in it. So the working of the findGCD() function is −

findGCD(78, 46): x = 0 y = max(78, 46) = 78 z = min(78, 46) = 46 while 46 != 0 { x = 78 y = 46 z = x % y => 78 %46 = 32 } while 32 != 0 { x = 78 y = 32 z = x % y => 78 %32 = 14 } while 14 != 0 { x = 32 y = 14 z = x % y => 32 % 14 = 4 } while 4 != 0 { x = 14 y = 4 z = x % y => 14 % 4 = 2 } while 2 != 0 { x = 4 y = 2 z = x % y => 4 % 2 = 0 } return y => 2

So display the output the GCD of 78 and 46 is 2.

## Method 2 - Recursive Method

We can calculate GCD using the recursive method.

### Algorithm

Following is the algorithm −

**Step 1**− Create a function with two arguments.**Step 2**− Declare a int type variable named res to store the reminder.

let res: Int = num1 % num2

**Step 3**− Check if res != 0. If condition is true then call the findGCD() function recursively. Otherwise return num2 because the reminder is zero which means we find the GCD of two number.**Step 4**− Call the function and store the result in “result” variable.**Step 5**− Print output.

### Example

The following program shows how to find GCD of two numbers using recursive approach.

import Swift import Foundation // Function to find gcd of two numbers func findGCD(num1: Int, num2: Int) -> Int { let res: Int = num1 % num2 if res != 0{ return findGCD(num1: num2, num2: res) } else{ return num2 } } // Calling Function var result = findGCD(num1:12, num2:8) print("GCD of 12 and 8 is ", result)

### Output

GCD of 12 and 8 is 4

Here, in the above code, we create a function named findGCD() to find the GCD of two numbers. This function also follows Euklid's algorithm. Here in this function, we find the reminder of two numbers by call the function itself. Now call the findGCD() function and pass 12 and 8 as an arguments in it. So the working of the findGCD() function is −

findGCD(12, 8): res = num1 % num2 = 12 % 8 = 4 if 4 != 0{ return findGCD(num1: 8, num2: 4) } else{ return num2 } res = 8 % 4 = 0 if 0 != 0{ return findGCD(num1: 8, num2: 4) } else{ return num2 => 4 }

So display the output GCD of 12 and 8 is 4.

- Related Articles
- Swift program to find the GCD of two given numbers using recursion
- Java Program to Find GCD of two Numbers
- Kotlin Program to Find GCD of two Numbers
- Haskell program to find the gcd of two numbers
- Find GCD of two numbers
- Swift Program to Find LCM of two Numbers
- Java program to find the GCD or HCF of two numbers
- Program to find GCD or HCF of two numbers in C++
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- Haskell Program to find the GCD of two given numbers using recursion
- Program to compute gcd of two numbers recursively in Python
- Program to find GCD of floating point numbers in C++
- C program to find GCD of numbers using recursive function
- Program to find GCD or HCF of two numbers using Middle School Procedure in C++
- C++ Program to Find the GCD and LCM of n Numbers