LCM of two numbers in C

05 min read

C program to find the LCM (Lowest Common Multiple) of two numbers is given below. The LCM of two integers is the smallest positive integer that is perfectly divisible by both the given numbers. So now every number has to be checked if it is divisible by given two numbers.

#include <stdio.h>
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
min= (n1>n2) ? n1 : n2;  //maximum number between given n1 and n2 gets stored in min
while(1)
{
if( min%n1==0 && min%n2==0 )
{
printf("The LCM of %d and %d is %d.", n1, n2,min);
break;
}
++min;
}
return 0;
}

 

Output:

Enter two positive integers: 72 120 The LCM of 72 and 120 is 360.
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error