GCD LCM

/*
@startuml
class GcdLcm {
    +main(String[] args)
    -getIntegerInput(Scanner scan, String prompt) : int
    -computeGCD(int a, int b) : int
    -computeLCM(int a, int b, int gcd) : int
}

class InputHandler {
    +validateInteger(String input) : boolean
    +matchesExitCommand(String input) : boolean
}

class UserInteraction {
    +askToPlayAgain(Scanner scan) : boolean
}

class Scanner

GcdLcm ..> InputHandler : uses
GcdLcm ..> UserInteraction : uses
GcdLcm ..> Scanner : "read input"
UserInteraction ..> Scanner : "read input"
@enduml
 */

import java.util.Scanner;

public class GcdLcm {

    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            System.out.println("\n----------Welcome to GCD & LCM Applet----------\n");

            while (true) {
                int a, b, remainder, LCM, GCD;

                // Get the first integer
                while (true) {
                    System.out.println("Please enter 1st integer or 'bye' to quit.");
                    String input = scan.nextLine().trim();

                    // Use regex `(?i)` to match multiple exit commands, `.*` allows matching whole sentences
                    if (input.matches("(?i).*(bye|exit|quit|b).*")) {
                        System.out.println("Goodbye.");
                        return;
                    }

                    try { // Try-catch for integer parsing
                        a = Integer.parseInt(input);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.println("Invalid input!");
                    }
                }

                // Get the second integer
                while (true) {
                    System.out.println("Please enter 2nd integer or 'bye' to quit.");
                    String input = scan.nextLine().trim();

                    // Use `equalsIgnoreCase()` for a simple exact match
                    if (input.equalsIgnoreCase("bye")) {
                        System.out.println("Goodbye.");
                        return;
                    }

                    // Use regex `-?\\d+` to match integers (including negatives)
                    if (input.matches("-?\\d+")) {
                        b = Integer.parseInt(input);
                        if (a == 0 && b == 0) {
                            System.out.println("Both numbers cannot be zero at the same time.");
                        } else {
                            break;
                        }
                    } else {
                        System.out.println("Invalid input.");
                    }
                }

                // Store original values for GCD calculation
                int tempA = a;
                int tempB = b;

                // Compute GCD using Euclidean Algorithm
                while (tempB != 0) {
                    remainder = tempA % tempB;
                    tempA = tempB;
                    tempB = remainder;
                }
                GCD = Math.abs(tempA);

                // Compute LCM using original a and b
                LCM = (a == 0 || b == 0) ? 0 : Math.abs((a * b) / GCD);

                // Output
                System.out.println("--------------------Results--------------------\n");
                System.out.println("The 1st number is: " + a + ", the 2nd number is: " + b);
                System.out.println("\nTheir GCD is: " + GCD + ", their LCM is: " + LCM);
                System.out.println("-----------------------------------------------\n");

                // Use `switch-case` to handle the replay prompt
                while (true) {
                    System.out.println("Do you want to play again? (y/n)");
                    String playAgain = scan.nextLine().trim();

                    switch (playAgain.toLowerCase()) {
                        case "n" -> {
                            System.out.println("Program ended. Goodbye!");
                            return;
                        }
                        case "y" -> {
                            break; // re-enter the outer while-loop
                        }
                        default -> {
                            System.out.println("Invalid input.");
                            continue;
                        }
                    }
                    break; // break replay input loop
                }
            }
        }
    }
}

 

Scroll to Top