C Programming Code Examples C > For Loops and While Loops Code Examples C program to find LCM of two numbers C program to find LCM of two numbers Write a C program to input two numbers from user and find LCM (Lowest Common Multiple) using loop. How to find LCM of two given numbers in C programming. Logic to find LCM of two numbers Input two numbers from user. Store them in some variable say j1 and j2. Find maximum between two numbers. Store the result in some variable, say max. Maximum is used to generate next multiple which must be common to both. If max is exactly divisible by both numbers. Then you got your answer, store max to some variable say lcm = max. If LCM is found then terminate from loop using break keyword. If max is not divisible by both numbers. Then generate next multiple of max. Repeat steps 2 to 3 step till LCM is found. #include <stdio.h> int main() { int i, j1, j2, max, lcm=1; /* Input two numbers from user */ printf("Enter any two numbers to find LCM: "); scanf("%d%d", &j1, &j2); /* Find maximum between j1 and j2 */ max = (j1 > j2) ? j1 : j2; /* First multiple to be checked */ i = max; /* Run loop indefinitely till LCM is found */ while(1) { if(i%j1==0 && i%j2==0) { /* * If 'i' divides both 'j1' and 'j2' * then 'i' is the LCM. */ lcm = i; /* Terminate the loop after LCM is found */ break; } /* * If LCM is not found then generate next * multiple of max between both numbers */ i += max; } printf("LCM of %d and %d = %d", j1, j2, lcm); return 0; }