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



LCM :- The least common multiple (LCM) of two numbers is the natural number that is a multiple of both.
For Example :-
Input = 3 and 4
Output = 12 is the LCM.
So as we can see that least common multiple (LCM) of two numbers 3 and 4 is 12.So, the LCM of 3 and 4 is 12.

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, goto step 3
STOP


LCM Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main()
    {
    int num1=10, num2=20, i, gcd,lcm;
    for(i=1; i <= num1 && i <= num2; ++i)
    {
    // Checks if i is factor of both integers
    if(num1%i==0 && num2%i==0)
    gcd = i;
    }
    lcm = (num1 * num2) / gcd;
    printf("LCM of %d and %d is %d", num1, num2, lcm);
    return 0;
    }
    
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    int num1=10, num2=20,i,gcd,lcm;
    for(i=1; i <= num1 && i <= num2; ++i)
    {
    // Checks if i is factor of both integers
    if(num1%i==0 && num2%i==0)
    gcd = i;
    }
    lcm = (num1 * num2) / gcd;
    cout << "LCM of "<< num1<<" and "<< num2<<" is "<< lcm;
    return 0;
    }
    
    public class LFC {
    public static void main(String[] args) {
    int num1 = 10, num2 = 20, gcd = 1,lcm;
    for(int i = 1; i <= num1 && i <= num2; ++i)
    {
    // Checks if i is factor of both integers
    if(num1 % i==0 && num2 % i==0)
    gcd = i;
    }
    lcm = (num1 * num2) / gcd;
    System.out.printf("LCM of %d and %d is %d", num1, num2, lcm);
    }
    }
    
    def find_lcm(num1, num2):
    # choose the smaller number
    if num1 > num2:
    smaller = num2
    else:
    smaller = num1
    for i in range(1, smaller+1):
    if((num1 % i == 0) and (num2 % i == 0)):
    gcd = i 
    return gcd
    num1 = 10
    num2 = 20
    gcd=find_lcm(num1, num2)
    lcm = (num1 * num2) / gcd;
    print ("LCM of ", num1, " and ", num2, " is ", lcm)
    
    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,x,y,lcm; 
    x = num1;
    y = num2;
    while (num1 != num2)
    {
    if (num1 > num2)
    {
    num1 = num1 - num2;
    }
    else
    {
    num2 = num2 - num1;
    }
    }
    lcm = (x * y) / num1;
    Console.Write("LCM of " 
    + x +" and " + y + " is " + lcm);
    Console.Read();
    }
    }
    }
    
    function find_gcd( $num1, $num2) 
    { 
    if ($num1 == 0) 
    return $num2; 
    return find_gcd($num2 % $num1, $num1); 
    } 
    // Function to return LCM 
    // of two numbers 
    function find_lcm( $num1, $num2) 
    { 
    return ($num1 * $num2) / find_gcd($num1, $num2); 
    } 
    // Driver Code 
    $num1 = 10;  
    $num2 = 20; 
    echo "LCM of ",$num1, " and "
    ,$num2, " is ", find_lcm($num1, $num2); 
    
    #include <stdio.h>
    int main()
    {
    int num1=10, num2=20, i, gcd,lcm;
    for(i=1; i <= num1 && i <= num2; ++i)
    {
    // Checks if i is factor of both integers
    if(num1%i==0 && num2%i==0)
    gcd = i;
    }
    lcm = (num1 * num2) / gcd;
    printf("LCM of %d and %d is %d", num1, num2, lcm);
    return 0;
    }
    

    Output

    LCM of 10 and 20 is 20
    

    Recommended Programs

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