-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from Tony-91/lcmProgram
RE: Java Program to Find LCM of two Numbers #17
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) | ||
|