Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update LCM of 2 numbers #61

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions LCM of 2 numbers
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
import java.util.Scanner;

public class LCMCalculator {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the two numbers
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();

// Calculate the LCM
int lcm = calculateLCM(num1, num2);

// Display the LCM
System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);

// Close the scanner
scanner.close();
};

// Function to calculate the LCM of two numbers
public static int calculateLCM(int num1, int num2) {
// Calculate the GCD using the Euclidean algorithm
int gcd = calculateGCD(num1, num2);

// Calculate the LCM using the formula (a * b) / GCD(a, b)
int lcm = (num1 * num2) / gcd;

return lcm;
};

// Function to calculate the GCD of two numbers using the Euclidean algorithm
public static int calculateGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
public class Calculator {

public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the two numbers
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();

// Calculate the LCM
int lcm = (num1 * num2) / calculateGCD(num1, num2);

// Display the LCM
System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);

// Close the scanner
scanner.close();
}

// Function to calculate the GCD of two numbers using the Euclidean algorithm
public static int calculateGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}