# Program to find LCM of two numbers

In this post, you will learn how to find the **LCM (Lowest common multiple)** of two numbers in different programming languages. Such a type of question is generally asked in programming interviews.

The **Least Common Multiple (LCM)** of two numbers is the smallest number that is a multiple of both of them. Or, the **LCM** of two numbers is the smallest common multiple or a positive integer which is divisible completely by both the numbers. Suppose the **LCM** of **a** and **b** is equal to **c**, then **c** should be evenly divisible by both **a** and **b**. For two integers **a**, **b**, the least common multiple of **a** and **b** is denoted **lcm(a,b)**.

## Java program to calculate LCM using GCD

In the given example, we use the for loop and divide both the numbers by **i**. If the remainder is 0, the number is completely divisible by **i**. After finding the **gcd**, we can easily calculate the LCM of two numbers using the formula **(a * b) / gcd**.

```
public class LcmProgram
{
public static void main(String args[])
{
int a = 26, b = 4, gcd = 1;
// finding GCD
for(int i = 1; i <= a && i <= b; ++i)
{
if(a % i == 0 && b % i == 0)
//assigns i into gcd
gcd = i;
}
// finding lcm of the given number
int lcm = (a * b) / gcd;
// prints the result
System.out.printf("The LCM of %d and %d is %d.", a, b, lcm);
}
}
```

**Output of the above code:**

`The LCM of 26 and 4 is 52.`

## C program to calculate LCM using while and if statement

Here is the C program to calculate the LCM using while loop and if statement. The numbers entered by the user are stored in variable **num1** and **num2**. The largest number among **num1** and **num2** is stored in **max**. The if statement within the loop is used to check whether the value in the **max** variable is divisible by both the numbers. If the **max** is perfectly divisible by **num1** and **num2**, the final **LCM** value is printed.

```
#include <stdio.h>
int main() {
int num1, num2, max;
printf("Enter two positive numbers: ");
scanf("%d %d", &num1, &num2);
// finding maximum number between num1 and num2
max = (num1 > num2) ? num1 : num2;
while (1) {
if (max % num1 == 0 && max % num2 == 0) {
printf("The LCM of %d and %d is %d.", num1, num2, max);
break;
}
++max;
}
return 0;
}
```

**Output of the above code:**

```
Enter two positive numbers: 24 16
The LCM of 24 and 16 is 48.
```

## C++ program to calculate LCM using do while loop

Here is the **C++** program to calculate the **LCM** using do while loop and if statement.

```
#include <iostream>
using namespace std;
int main()
{
int num1, num2, max;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
// finding maximum value between num1 and num2
max = (num1 > num2) ? num1 : num2;
do
{
if (max % num1 == 0 && max % num2 == 0)
{
cout << "LCM = " << max;
break;
}
else
++max;
} while (true);
return 0;
}
```

**Output of the above code:**

```
Enter two numbers: 45 50
LCM = 450
```

## Python program to calculate LCM using while and if

Here is the **Python** program to calculate the **LCM** using a **while loop** and an **if** statement. We use an if statement to find out which of the numbers is smaller and then use a while loop whose condition is always true (or 1) unless a break is used. Then an if statement within the loop is used to check whether the value in the minimum variable is divisible by both the numbers. If it is divisible, the break statement breaks out of the loop, and if it is not divisible, the value in the minimum variable is incremented. The final LCM value is printed.

```
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if(num1> num2):
min1=num1
else:
min1=num2
while(1):
if(min1%num1==0 and min1%num2==0):
print("LCM = ",min1)
break
min1=min1+1
```

**Output of the above code:**

```
Enter the first number: 54
Enter the second number: 26
LCM = 702
```

### Related Articles

**Java program to find area of triangle**

Area of circle program in Java

Remove duplicate elements from array in Java

Capitalize first letter of each word Java

Convert binary to decimal in Java

Convert array to list Python

Python take screenshot of specific window

Web scraping Python BeautifulSoup

Check if two strings are anagrams Python

Python program to add two numbers

Print new line python

Prime factors of a number in c

Armstrong number program in c

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to convert Decimal to Octal

C program to convert decimal to binary

C program to check whether a number is even or odd

Area of circle program in Java

Remove duplicate elements from array in Java

Capitalize first letter of each word Java

Convert binary to decimal in Java

Convert array to list Python

Python take screenshot of specific window

Web scraping Python BeautifulSoup

Check if two strings are anagrams Python

Python program to add two numbers

Print new line python

Prime factors of a number in c

Armstrong number program in c

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to convert Decimal to Octal

C program to convert decimal to binary

C program to check whether a number is even or odd