C# Program - Find LCM of Two Numbers without GCD
LCM stands for Least Common Multiple. The LCM of two numbers is the smallest number that can be divided by both numbers.
For example - LCM of 20 and 25 is 100 and LCM of 30 and 40 is 120.
The approach is to start with the largest of the two numbers and keep incrementing the larger number by itself till it becomes divisible by the smaller number.
Example: find LCM of two numbers without GCD
In the example below, to calculate the LCM of two numbers, largest of the two numbers is increased by itself till it becomes divisible by the smaller number.
using System; class MyProgram { static int lcm(int x, int y) { int i, large, small; large = Math.Max(x,y); small = Math.Min(x,y); i = large; while (true) { if(i % small == 0) return i; i = i + large; } } static void Main(string[] args) { int x = 30; int y = 40; Console.WriteLine("LCM of "+ x +" and "+ y +" is: "+ lcm(x,y)); } }
The above code will give the following output:
LCM of 30 and 40 is: 120
Recommended Pages
- C# Program - To Check Prime Number
- C# Program - Bubble Sort
- C# Program - Selection Sort
- C# Program - Maximum Subarray Sum
- C# Program - Reverse digits of a given Integer
- C# - Swap two numbers
- C# Program - Fibonacci Sequence
- C# Program - Insertion Sort
- C# Program - Find Factorial of a Number
- C# Program - Find HCF of Two Numbers
- C# Program - Merge Sort
- C# Program - Shell Sort
- Stack in C#
- Queue in C#
- C# Program - Find LCM of Two Numbers
- C# Program - To Check Armstrong Number
- C# Program - Counting Sort
- C# Program - Radix Sort
- C# Program - Find Largest Number among Three Numbers
- C# Program - Print Floyd's Triangle