Q. Write a program to find LCM of two numbers.



LCM :- LCM (Least Common Multiple) of numbers is defined as the smallest possible multiple of two or more numbers.

For Example :-
Input = 3 and 4
Output = 12 is the LCM.
As we can see 12 is the smallest possible multiple of 3 and 4 therefore, 12 is the least common multiple (LCM) of two numbers 3 and 4.

LCM Algorithm

START
Step 1 → Initialize num1 and num2 with positive integers
Step 2 → Store maximum of num1 & num2 to max
Step 3 → Check if max is divisible by num1 and num2
Step 4 → If divisible, Display max as LCM
Step 5 → If not divisible then increase max by 1 and then goto step 3
STOP


LCM Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main()
    {
    int num1=10, num2=20, max;
    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;
    }
    
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    int num1=10, num2=20, max;
    max = (num1 > num2) ? num1 : num2;
        while (1) {
            if (max % num1 == 0 && max % num2 == 0) {
                cout << "LCM of "<< num1<<" and "<< num2<<" is "<< max;
                break;
            }
            max++;
        }
    return 0;
    }
    
    public class LFC {
    public static void main(String[] args) {
    	
    	int num1 = 10, num2 = 20, max;
    	max = (num1 > num2) ? num1 : num2;
    
        while(true) {
          if( max % num1 == 0 && max % num2 == 0 ) {
            System.out.printf("The LCM of %d and %d is %d.", num1, num2, max);
            break;
          }
          max++;
    	}
    }
    }
    
    
    num1 = 10
    num2 = 20
    if num1 > num2:
       max = num1
    else:
       max = num2
    
    while(True):
       if((max % num1 == 0) and (max % num2 == 0)):
    	   break
       max += 1
    print("The LCM of given number is ", max)
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication9
    {
    class LFC
    {
    public static void Main(string[] args)
    {
    int num1 = 10, num2 = 20, max, lcm; 
    max = (num1>num2) ? num1 : num2;  
        for(i=max;  ; i+=max)  
        {  
            if(i%num1==0 && i%num2==0)  
            {  
                lcm = i;  
                break;  
            }  
        }  
        Console.Write("\nLCM of {0} and {1} = {2}\n\n", num1, num2, lcm);
    }
    }
    }
    
    $num1 = 10;
    $num2 = 20;
    $max = 0;
    
    $max = $num1 > $num2 ? $num1 : $num2;
    
    while (1) {
        if ($max % $num1 == 0 && $max % $num2 == 0) {
            echo "The LCM of the {$num1} and {$num2} numbers is {$max}.";
            break;
        }
        ++$max;
    }
    
    #include <stdio.h>
    int main()
    {
    int num1=10, num2=20, max;
    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

    LCM of 10 and 20 is 20
    




    Recommended Programs

       Program to find GCD of 2 Numbers.
       Program to check Vowels & Consonant.