- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find the LCM of two numbers
L.C.M. or Least Common Multiple of two values is the smallest positive value which the multiple of both values.
For example multiples of 3 and 4 are:
3 → 3, 6, 9, 12, 15 ... 4 → 4, 8, 12, 16, 20 ...
The smallest multiple of both is 12, hence the LCM of 3 and 4 is 12.
Algorithm
- Initialize A and B with positive integers.
- Store maximum of A & B to the max.
- Check if max is divisible by A and B.
- If divisible, Display max as LCM.
- If not divisible then step increase max, go to step 3.
Example
public class LCMOfTwoNumbers { public static void main(String args[]){ int a, b, max, step, lcm = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter first number ::"); a = sc.nextInt(); System.out.println("Enter second number ::"); b = sc.nextInt(); if(a > b){ max = step = a; } else{ max = step = b; } while(a!= 0) { if(max % a == 0 && max % b == 0) { lcm = max; break; } max += step; } System.out.println("LCM of given numbers is :: "+lcm); } }
Output
Enter first number :: 6 Enter second number :: 10 LCM of given numbers is :: 30
- Related Articles
- Java Program to Find LCM of two Numbers
- Swift Program to Find LCM of two Numbers
- Haskell program to find lcm of two numbers
- Kotlin Program to Find LCM of two Numbers
- Program to find LCM of two Fibonnaci Numbers in C++
- Find LCM of two numbers
- GCD and LCM of two numbers in Java
- Java Program to Find GCD of two Numbers
- C++ Program to Find the GCD and LCM of n Numbers
- Java program to find the GCD or HCF of two numbers
- Java Program to Find the Product of Two Numbers Using Recursion
- How to find the LCM of two given numbers using Recursion in Golang?
- The product of two numbers is 1944 and their LCM is 108. Find the HCF of the two numbers.
- Java Program to Add the two Numbers
- Find HCF and LCM of 404 and 96 and verify that HCF$×$LCM $ =$ Product of the two given numbers.

Advertisements