Big GCD

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

class InputHandler {
    +validateBigInteger(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.math.BigInteger;
import java.util.Scanner;

public class BigGcd {

    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) {
                BigInteger a, b, LCM;

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

                    if (input.matches("(?i)\\b(bye|exit|quit|b)\\b")) {
                        System.out.println("Goodbye.");
                        return;
                    }

                    try {
                        a = new BigInteger(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();

                    if (input.matches("(?i)\\b(bye|exit|quit|b)\\b")) {
                        System.out.println("Goodbye.");
                        return;
                    }

                    try {
                        b = new BigInteger(input);
                        if (a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) {
                            System.out.println("Both numbers cannot be zero at the same time.");
                        } else {
                            break;
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("Invalid input!");
                    }
                }

                // Compute GCD and LCM
                BigInteger gcd = a.gcd(b);
                LCM = (a.equals(BigInteger.ZERO) || b.equals(BigInteger.ZERO))
                        ? BigInteger.ZERO
                        : a.multiply(b).abs().divide(gcd);

                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");

                // Ask to Play Again
                while (true) {
                    System.out.println("Do you want to play again? (y/n)");
                    String playAgain = scan.nextLine().trim().toLowerCase();

                    switch (playAgain) {
                        case "n" -> {
                            System.out.println("Program ended. Goodbye!");
                            return;
                        }
                        case "y" -> {
                            break;
                        }
                        default -> {
                            System.out.println("Invalid input. Please enter 'y' or 'n'.");
                            continue;
                        }
                    }
                    break;
                }
            }
        }
    }
}

 

Scroll to Top