Write a program to find LCM of two numbers

14 May 2022

Write a program to find LCM of two numbers


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

 

Description

Get two numbers from the user and find the least common multiple of two numbers

Input

6

Output

6

C Program

Method 1

#include <stdio.h>

int main()

{

    int num1,num2,lcm;

    printf("Enter two numbers: ");

    scanf("%d %d", &num1,&num2);

        int max=(num1 > num2)? num1 : num2;

        for(int i=max;i<=num1*num2;i++)

        {

            if(i%num1==0 && i%num2==0)

            {

                lcm=i;

                break;

            }

        }

    printf(lcm);

    return 0;

}

Method 2

#include <stdio.h>

int main()

{

    int num1,num2,hcf=1;

    printf("Enter two numbers: ");

    scanf("%d %d", &num1,&num2);

    for(int i=1;i<=num1 || i<=num2;i++)

    {

        if(num1%i==0 && num2%i==0)

            hcf = i;

    }

    int lcm = (num1*num2)/hcf;

    printf("%d",lcm);

    return 0;

}

Method 3

#include <stdio.h>

int HCF(int num1, int num2)

{

    if (num1 == 0)

       return num2;

    if (num2 == 0)

       return num1;

    if (num1 == num2)

        return num1;

    if (num1 > num2)

        return HCF(num1 - num2, num2);

    return HCF(num1, num2 - num1);

}

 

int main()

{

    int num1,num2;

    printf("Enter two numbers: ");

    scanf("%d %d", &num1,&num2);

    int hcf = HCF(num1, num2);

    int lcm = (num1*num2)/hcf;

    printf("%d", lcm);

    return 0;

}

C++ Program

Method 1

#include <iostream>

using namespace std;

int main()

{

    int num1,num2,lcm;

    cout<<"Enter two numbers: ";

    cin>>num1>>num2;

        int max=(num1 > num2)? num1 : num2;

        for(int i=max;i<=num1*num2;i++)

        {

            if(i%num1==0 && i%num2==0)

            {

                lcm=i;

                break;

            }

        }

    cout<<lcm;

    return 0;

}

Method 2

#include <iostream>

using namespace std;

int main()

{

    int num1,num2,hcf=1;

    cout<<"Enter two numbers: ";

    cin>>num1>>num2;

    for(int i=1;i<=num1 || i<=num2;i++)

    {

        if(num1%i==0 && num2%i==0)

            hcf = i;

    }

    int lcm = (num1*num2)/hcf;

    printf("%d",lcm);

    return 0;

}

Java Program

Method 1

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);

    System.out.print("Enter two number: ");

    int num1=sc.nextInt();

    int num2=sc.nextInt();

    int lcm=1;

    int max = (num1 > num2) ? num1 : num2;

    for (int i = max; i <= num1 * num2; i++)

      {

     if (i % num1 == 0 && i % num2 == 0)

        {

         lcm = i;

         break;

        }

      }

    System.out.println (lcm);

}

}

Method 2

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);

    System.out.print("Enter two number: ");

    int num1=sc.nextInt();

    int num2=sc.nextInt();

    int lcm=1;

    int max = (num1 > num2)? num1:num2;

    for(int i = max ; i <= num1*num2 ; i=i+max)

        {

            if(i%num1==0 && i%num2==0){

                lcm = i;

                break;

            }

        }

    System.out.println (lcm);

}

}

Python

Method 1

num1=int(input("Enter first number: "))

num2=int(input("Enter second number: "))

for i in range(max(num1,num2),(num1*num2)+1):

    if (i%num1)==(i%num2)==0:

        lcm=i

        break

print(lcm)

Method 2

num1=int(input("Enter first number: "))

num2=int(input("Enter second number: "))

for i in range(max(num1, num2),(num1*num2)+1, max(num1, num2)):

    if (i%num1)==(i%num2)==0:

        lcm=i

        break

print(lcm)

Method 3

num1=int(input("Enter first number: "))

num2=int(input("Enter second number: "))

hcf=1

for i in range(1,max(num1, num2)):

    if (num1%i)==(num2%i)==0:

        hcf=i

lcm = (num1*num2)//hcf

print(lcm)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !