Home >>c programs >LCM Program in C

LCM Program in C

C Program to Find LCM of two Numbers

LCM program in C is basically written to calculate the L.C.M. of two numbers. Let's understand the definition of LCM and understand what it does. The LCM elaborates to Lowest Common Multiple.

The LCM of two integers n1 and n2 is basically the smallest positive integer that is perfectly divisible by both num1 and num2 that simply means that it should not leave a remainder.

Finding the LCM of two numbers in C is extremely easy as it involves the two methods that are if-else statement and while and do-while loop. You will find it very easy to derive the LCM in C as it is done with the help of various operators.

Here is a C program to find the LCM of two numbers that will help you understand the application aspect of it:

#include <stdio.h>
int main() 
{
    int num1, num2, minNum;
    printf("Enter your any two positive integer number ");
    scanf("%d %d", &num1, &num2);
    minNum = (num1 > num2) ? num1 : num2;
    while (1) 
    {
        if (minNum % num1 == 0 && minNum % num2 == 0) 
		{
            printf("Here is the  LCM of %d and %d is %d.", num1, num2, minNum);
            break;
        }
        ++minNum;
    }
    return 0;
}

Output :
Enter your any two positive integer number 5 10
Here is the LCM of 5 and 10 is 10
Output :
Enter your any two positive integer number 3 6
Here is the LCM of 3 and 6 is 6

No Sidebar ads