C Program to Find LCM of Two Numbers using While loop
ADVERTISEMENTS
C program to find lcm of two numbers using while loop. In this article, you will learn how to find lcm of two numbers in c language using while loop.
C Program to Find LCM of Two Numbers using While loop
// C Program to Find LCM of Two Numbers using While loop
#include <stdio.h>
int main() {
int p, q, x;
// p & q - To store the two positive numbers
// x - To store the LCM number
printf("-----Enter the two positive integer numbers-----\n");
scanf("%d %d", &p, &q);
x = p > q ? p : q;
while (1) {
if (x % p == 0 && x % q == 0) {
printf("\nThe LCM of the %d and %d numbers is %d.\n", p, q, x);
break;
}
++x;
}
return 0;
}
Output
-----Enter the two positive integer numbers-----
80
110
The LCM of the 80 and 110 numbers is 880.
C Program to Find LCM of Two Numbers using For loop
// C Program to Find LCM of Two Numbers using For loop
#include <stdio.h>
int main() {
int p, q, i, g, x;
// p & q - To store the two positive numbers
// x - To store the LCM number
// g - To store the GCD
printf("-----Enter the two positive integer numbers-----\n");
scanf("%d %d", &p, &q);
for (i = 1; i <= p && i <= q; ++i) {
if (p % i == 0 && q % i == 0) {
g = i;
}
}
x = (p * q) / g;
printf("\nThe LCM of two positive numbers %d and %d is %d.\n", p, q, x);
return 0;
}
Output
-----Enter the two positive integer numbers-----
80
110
The LCM of two positive numbers 80 and 110 is 880.