1. Introduction
The Least Common Multiple (LCM) of two integers is the smallest integer that is divisible by both numbers. In this guide, we will explore a Swift program to find the LCM of two numbers using the formula:(a * b) / gcd(a, b), wheregcd is the Greatest Common Divisor.
2. Program Overview
We'll begin by defining two numbers. To find the LCM, we will first compute the GCD (using Euclid's algorithm) and then use the aforementioned formula.
3. Code Program
// Define the two numberslet num1 = 15let num2 = 20// Function to compute the GCD of two numbersfunc gcd(_ a: Int, _ b: Int) -> Int { if b == 0 { return a } else { return gcd(b, a % b) }}// Function to compute the LCM of two numbersfunc lcm(_ a: Int, _ b: Int) -> Int { return (a * b) / gcd(a, b)}let result = lcm(num1, num2)print("LCM of \(num1) and \(num2) is: \(result)")
Output:
LCM of 15 and 20 is: 60
4. Step By Step Explanation
1.let num1 = 15 andlet num2 = 20: These are the two numbers for which we want to find the LCM.
2. The functiongcd calculates the Greatest Common Divisor of two numbers using Euclid's algorithm. This algorithm works based on the principle that the GCD of two numbers also divides their difference.
3. The functionlcm calculates the Least Common Multiple using the formula:(a * b) / gcd(a, b). The formula stems from the relationship between the GCD and the LCM of two numbers.
4. We then call thelcm function with our two numbers as arguments and print the result.
This program provides an efficient way to compute the LCM, especially for large numbers, by first calculating the GCD.
Related Swift Examples:
Swift Hello World ProgramSwift Program to Add Two NumbersSwift Program to Subtract Two NumbersSwift Program to Multiply Two NumbersSwift Program to Divide Two NumbersSwift Program to Find RemainderSwift Program to Check Even or OddSwift Program to Find Factorial of a NumberSwift Program to Generate Fibonacci SeriesSwift Program to Swap Two Numbers Without Using Temporary VariableSwift Program to Find Largest Among Three NumbersSwift Program to Calculate the Area of a CircleSwift Program to Reverse a NumberSwift Program to Make a Simple CalculatorSwift Program to Check PalindromeSwift Program to Count Number of Digits in an IntegerSwift Program to Sum of Natural NumbersSwift Program to Display Times TableSwift Program to Check Prime NumberSwift Program to Find LCMSwift Program to Find GCDSwift Program to Find the Power of a NumberSwift Program to Split a String into WordsSwift Program to Check Leap YearSwift Program to Join Two StringsSwift Program to Check Armstrong NumberSwift Program to Find Sum of Array ElementsSwift Program to Find the Largest Element of an ArraySwift Program to Perform Matrix AdditionSwift Program to Transpose a MatrixSwift Program to Multiply Two MatricesSwift Program to Find Length of a StringSwift Program to Copy One String to Another StringSwift Program to Concatenate Two StringsSwift Program to Search for a Character in a StringSwift Program to Count Frequency of a Character in StringSwift Program to Create a Simple Class and ObjectSwift Program to Implement InheritanceSwift Program to Handle Simple ExceptionsSwift Variables and Constants ExampleSwift Data Types (Int, Double, String) ExampleSwift Optionals and Optional Binding ExampleSwift Tuples ExampleSwift Array ExampleSwift Dictionary ExampleSwift Set ExampleSwift Closures ExampleSwift Enums ExampleSwift Structures ExampleSwift Properties (Stored, Computed) ExampleSwift Methods (Instance, Type) ExampleSwift Subscripts ExampleSwift Inheritance and Overriding ExampleSwift Protocols ExampleSwift Extensions ExampleSwift Generics and Generic Functions ExampleSwift Error Handling with Do-Catch ExampleSwift Guard Statement ExampleSwift Defer Statement ExampleSwift Type Casting (as, is, as?) ExampleSwift Access Control ExampleSwift Attributes (@available, @discardableResult) ExampleSwift Pattern Matching ExampleSwift Switch Statement and Cases ExampleSwift For-In Loop ExampleSwift While and Repeat-While Loops ExampleSwift Conditional Statements (If, If-Else, Ternary) ExampleSwift Operators ExampleSwift Memory Management ExampleSwift Strong, Weak, and Unowned References ExampleSwift Initialization and Deinitialization ExampleSwift Protocol-Oriented Programming ExampleSwift Nested Types ExampleSwift Type Aliases ExampleSwift Dynamic Member Lookup ExampleSwift Lazy Stored Properties ExampleSwift KeyPaths ExampleSwift String Manipulation and Methods ExampleSwift Regular Expressions Example SwiftSwift Programs
Comments
Post a Comment