GCD and LCM Calculator in Java

 

Introduction

In mathematics, the Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them without leaving a remainder. Similarly, the Least Common Multiple (LCM) is the smallest number that is divisible by both numbers.

In this program, we will calculate the GCD and LCM of two numbers using Java. These two operations are frequently used in number theory and have applications in cryptography, data compression, and error detection.

Objective

The objective of this program is to:

    • Calculate the GCD of two input numbers using the Euclidean algorithm.
    • Calculate the LCM of two input numbers using the formula:
                    LCM(a, b) = |a * b| / GCD(a, b)

Java Program Code

            public class GCD_LCM_Calculator {

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

                // Method to calculate LCM using the formula
                public static int calculateLCM(int a, int b) {
                    return Math.abs(a * b) / calculateGCD(a, b);
                }

                // Main method to take input and display results
                public static void main(String[] args) {
                    java.util.Scanner sc = new java.util.Scanner(System.in);

                    // Taking two integers as input
                    System.out.print("Enter first number: ");
                    int num1 = sc.nextInt();

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

                    // Calculating GCD and LCM
                    int gcd = calculateGCD(num1, num2);
                    int lcm = calculateLCM(num1, num2);

                    // Displaying the results
                    System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);
                    System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);
                }
            }

Explanation of the Program

The program consists of three main parts:

  • GCD Calculation: The method calculateGCD uses the Euclidean algorithm, which iteratively replaces the larger number by its remainder when divided by the smaller number. This continues until one number becomes 0, and the other number is the GCD.
  • LCM Calculation: The method calculateLCM calculates the LCM using the formula: LCM(a, b) = |a * b| / GCD(a, b). It makes use of the previously calculated GCD.
  • Input and Output: The program prompts the user to enter two numbers. It then calculates the GCD and LCM using the two methods and displays the results.

How to Run the Program:

  • Copy the code into a text file with a .java extension, for example, GCD_LCM_Calculator.java.
  • Open a terminal or command prompt and navigate to the directory where the file is saved.
  • Compile the Java program by typing javac GCD_LCM_Calculator.java and pressing Enter.
  • Run the compiled program by typing java GCD_LCM_Calculator and pressing Enter.
  • Enter the two integers when prompted, and the program will display the GCD and LCM.
© 2024 Learn Programming. All Rights Reserved.

 

Leave a Reply

Your email address will not be published. Required fields are marked *