Program to calculate the LCM of two numbers

Write a C program to calculate the LCM of two numbers. or Write a program to  calculate the LCM of two numbers  in C.

Program in C

Code:

/*Write a C program to calculate the LCM of two numbers. or Write a program to calculate the LCM of two numbers Using C*/

#include<stdio.h>
int main()
{
    int num1,num2;
    printf("Enter two number to find L.C.M:");
    scanf("%d %d",&num1,&num2);
    int n1=num1;
    int n2=num2;
    while(num1!=num2)
    {
       if(num1>num2)
          num1=num1-num2;
       else
          num2=num2-num1;
    }
    int lcm=(n1*n2)/num1;
    printf("The L.C.M is %d",lcm);
}

Input/Output:
Enter two number to find L.C.M:
25
78
The L.C.M is 1950

Write a C++ program to calculate the LCM of two numbers. or Write a program to calculate the LCM of two numbers in C++.

Program in C++

Code:

/*Write a C++ program to calculate the LCM of two numbers. or Write a program to calculate the LCM of two numbers using C++*/

#include<iostream>
using namespace std;
int main()
{
    int num1,num2;
    cout<<"Enter two number to find L.C.M:";
    cin>>num1>>num2;
    int n1=num1;
    int n2=num2;
    while(num1!=num2)
    {
       if(num1>num2)
          num1=num1-num2;
       else
          num2=num2-num1;
    }
    int lcm=(n1*n2)/num1;
    cout<<"The L.C.M is "<<lcm;
}

Input/Output:
Enter two number to find L.C.M:
5
5
The L.C.M is 5

Write a JAVA program to calculate the LCM of two numbers. or Write a program to calculate the LCM of two numbers in Java.

Program in Java

Code:

/*Write a JAVA program to calculate the LCM of two numbers. or Write a program to calculate the LCM of two numbers using Java*/

import java.util.Scanner;
public class LcmOfTwoNumbers {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1,num2;
    System.out.println("Enter two number to find L.C.M:");
    num1=cs.nextInt();
    num2=cs.nextInt();
    int n1=num1;
    int n2=num2;
    while(num1!=num2)
    {
       if(num1>num2)
          num1=num1-num2;
       else
          num2=num2-num1;
    }
    int lcm=(n1*n2)/num1;
    System.out.println("The L.C.M is "+lcm);
cs.close();
}
}

Input/Output:
Enter two number to find L.C.M:
5
9
The L.C.M is 45

Write a PYTHON to calculate the LCM of two numbers. or Write a program to calculate the LCM of two numbers in Python.

Program in Python

Code:

'''Write a Python program to calculate the LCM of two numbers. or 
   Write a program to calculate the LCM of two numbers using Python '''

print("Enter two number to find L.C.M:")
num1=int(input())
num2=int(input())
n1=num1
n2=num2
while(num1!=num2):
   if (num1 > num2):
      num1 = num1 - num2
   else:
      num2= num2 - num1
lcm=int((n1*n2)/num1)
print("L.C.M is",lcm)

Input/Output:
Enter two number to find L.C.M:
42
40
L.C.M is 840 




Post a Comment

0 Comments