Find the LCM using Loops in C
To find the Least Common Multiple (LCM) of two numbers in C, we use loops to check for the smallest number that is evenly divisible by both numbers. The most common approach is using a loop to increment the maximum of the two numbers until we find a common multiple.
Examples of Finding LCM Using Loops
1. Finding LCM Using a while
Loop
In this example, we use a while
loop to iterate and find the smallest multiple of the two numbers.
main.c
</>
Copy
#include <stdio.h>
int main() {
int num1, num2, lcm;
// Input two numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Initialize LCM with the maximum of num1 and num2
lcm = (num1 > num2) ? num1 : num2;
// Loop to find the LCM
while (1) {
if (lcm % num1 == 0 && lcm % num2 == 0) {
printf("LCM of %d and %d is %d\n", num1, num2, lcm);
break;
}
lcm++; // Increment LCM value
}
return 0;
}
Explanation:
- We declare variables
num1
,num2
for input andlcm
to store the result. - We take input values for
num1
andnum2
usingscanf()
. - The variable
lcm
is initialized with the greater of the two numbers. - A
while
loop is used to continuously check iflcm
is divisible by both numbers. - Once we find the LCM, we print it and use
break
to exit the loop.
Output:
Enter two numbers: 4 6
LCM of 4 and 6 is 12
2. Finding LCM Using a for
Loop
In this example, we use a for
loop to iterate through numbers starting from the maximum of the two numbers.
main.c
</>
Copy
#include <stdio.h>
int main() {
int num1, num2, lcm, max;
// Input two numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Get the maximum of the two numbers
max = (num1 > num2) ? num1 : num2;
// Finding LCM using a for loop
for (lcm = max; ; lcm += max) {
if (lcm % num1 == 0 && lcm % num2 == 0) {
printf("LCM of %d and %d is %d\n", num1, num2, lcm);
break;
}
}
return 0;
}
Explanation:
- We declare variables
num1
,num2
,lcm
, andmax
. - We take user input for
num1
andnum2
usingscanf()
. - The variable
max
stores the greater of the two numbers. - A
for
loop starts atmax
and increments in steps ofmax
. - The loop breaks when we find the LCM, which is printed.
Output:
Enter two numbers: 5 10
LCM of 5 and 10 is 10
3. Finding LCM Using the GCD Formula
In this example, we calculate LCM using the formula: LCM(a, b) = (a * b) / GCD(a, b)
. We use a loop to find the Greatest Common Divisor (GCD) first.
main.c
</>
Copy
#include <stdio.h>
int main() {
int num1, num2, gcd, lcm, i;
// Input two numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Finding GCD using a loop
for (i = 1; i <= num1 && i <= num2; i++) {
if (num1 % i == 0 && num2 % i == 0) {
gcd = i;
}
}
// Using LCM formula
lcm = (num1 * num2) / gcd;
printf("LCM of %d and %d is %d\n", num1, num2, lcm);
return 0;
}
Explanation:
- We declare variables for
num1
,num2
,gcd
,lcm
, and an iteratori
. - We use a
for
loop to find the greatest common divisor (GCD) of the two numbers. - We compute LCM using the formula
(num1 * num2) / gcd
. - The result is printed.
Output:
Enter two numbers: 8 12
LCM of 8 and 12 is 24