Swift Program to Find LCM of two Numbers


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

LCM is also known as Least Common Multiple. It is used to calculate the smallest common multiple between two or more numbers. Here common multiple represents a number which is multiple of two or numbers. For example, we have two numbers 10 and 8

Factor of 10 = 2 x 5

Factor of 8 = 2 x 2 x 2

Union of both factors = 2 x 2 x 2 x 5 = 40

So the LCM of 10 and 8 is 40.

This is the simple method to find the LCM but the most efficient method to find LCM is using the following formula.

Formula

Following is the formula of LCM −

LCM(n, m) = n *m/gcd(n, m)

Here gcd is known as greatest common divisor.

Below is a demonstration of the same −

Input

Suppose ou r given input is −

Number 1 - 10
Number 2 - 8

Output

The desired output would be −

LCM 40

Algorithm

Following is the algorithm −

  • Step 1 − Create a function named findGCD(). This function returns the GCD of two numbers.

  • Step 2 − Create another function named findLCM. This function returns the LCM of two numbers.

return (n1 * n2/findGCD(n1, n2))
  • Step 3 − Call the findLCM() function and pass two arguments in it. Here the value of the arguments can be user-defined or pre-defined.

  • Step 4 − Print output

Example 1

The following program shows how to find LCM of two numbers.

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 } // Function to find lcm of two numbers func findLCM(n1: Int, n2: Int)->Int{ return (n1 * n2/findGCD(n1, n2)) } // Calling Function var result = findLCM(n1: 10, n2:8) print("LCM of 10 and 8 is ", result)

Output

LCM of 10 and 8 is 40

Here, in the above code, we create a function named findGCD () to find the GCD of two numb ers. We create another function named as findLCM() to find the LCM of two numbers using the mathematical formula −

return (n1 * n2/findGCD(n1, n2))

Now we call the findLCM() function and pass two arguments in it that is 10 and 8 and display the LCM of 10 an d 8 that is 40.

Example 2

The following program shows how to find LCM of two numbers.

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 } // Function to find lcm of two numbers func findLCM(n1: Int, n2: Int)->Int{ return (n1 * n2/findGCD(n1, n2)) } // Taking input from the user print("Please enter the value of number 1:") var number1 = Int(readLine()!)! print("Please enter the value of number 2:") var number2 = Int(readLine()!)! // Calling Function var result = findLCM(n1: number1, n2:number2) print("So the LCM of \(number1) and \(number2) is ", result)

STDIN Input

Please enter the value of number 1: 
26 
Please enter the value of number 2: 
35

Output

So the LCM of 26 and 35 is 910

Here, in the above code, we create a function named findGCD () to find the GCD of two numbers. We create another function named as findLCM() to find the LCM of two numbers using the mathematical formula −

return (n1 * n2/findGCD(n1, n2))

Here we take the two number of integer type from the user using readLine() function. And call the findLCM() function and pass these two numbers as an argument and display the output. Suppose user enter number1 = 26 and number2 = 35 so their LCM is 910.

Updated on: 18-Aug-2022

582 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements