Skip to content

Commit

Permalink
Merge pull request #147 from Tony-91/lcmProgram
Browse files Browse the repository at this point in the history
RE: Java Program to Find LCM of two Numbers #17
  • Loading branch information
sujana-kamasany authored Oct 5, 2023
2 parents be0a6c4 + 1496759 commit 5447992
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lcmProgram/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/* Lowest Common Multiple : smallest number that is divisible by two numbers.
*
* EXAMPLE : 18 and 36
* 2 * 3 * 3 = 18
* 2 * 2 * 3 * 3 = 36
* common factors = 2 * 2 * 3 * 3 = 36
* LCM = 36
* */

// Declare two integer variables to store input values
int number1;
int number2;

// Create a Scanner object to read input from user
Scanner scan = new Scanner(System.in);

// Prompt user to enter first number and read input
System.out.println("Enter first number : ");
number1 = scan.nextInt();

// Prompt user to enter second number and read input
System.out.println("Enter second number : ");
number2 = scan.nextInt();

// Initialize lcm variable to the maximum of the two input numbers
int lcm = Math.max(number1, number2);

// Loop until the LCM is found
while (true) {
// Check if lcm is divisible by both numbers
if (lcm % number1 == 0 && lcm % number2 == 0) {
// If lcm is found, print it and exit the loop
System.out.println("LCM = "+ lcm);
break;
}
// If lcm is not found, increment it and continue looping
++lcm;
}



}
}
8 changes: 8 additions & 0 deletions lcmProgram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Helpful Links

## [How to Execute and Run Java Code from the Terminal](https://www.freecodecamp.org/news/how-to-execute-and-run-java-code/)

## [Lowest Common Multiple refresher](https://www.khanacademy.org/math/cc-sixth-grade-math/cc-6th-expressions-and-variables/cc-6th-lcm/a/least-common-multiple-review)

## [The **Math.max()** method in Java](https://www.geeksforgeeks.org/java-math-max-method-examples/)

0 comments on commit 5447992

Please sign in to comment.