Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - GCD

Updated 20 Oct 2026

The GCD (Greatest Common Divisor), also called the HCF (Highest Common Factor), of two numbers is the largest number that divides both of them exactly. This program finds it with Euclid's algorithm, written as a recursive method.

Definition

The GCD of two positive integers $a$ and $b$ is the greatest integer that divides both $a$ and $b$ with no remainder.

  • The factors of 48 are 1, 2, 3, 4, 6, 8, 12, 16, 24, 48 and of 18 are 1, 2, 3, 6, 9, 18. The largest common factor is 6, so the GCD of 48 and 18 is 6.
  • 17 and 5 have only 1 in common, so their GCD is 1.

Euclid's algorithm uses the fact that the GCD of $a$ and $b$ is the same as the GCD of $b$ and the remainder of $a \div b$. The process stops when the remainder becomes 0, and the last non-zero number is the GCD.

Algorithm

  1. Start. Create an object of class GCD in main.
  2. accept() reads the two numbers into a and b.
  3. display() checks the input. If a or b is zero or negative, print an error message and stop.
  4. Otherwise display() calls hcf(a, b).
  5. In hcf(x, y):
    • If y is 0, return x (base case).
    • Otherwise return hcf(y, x % y) (recursive call, with the remainder replacing the second number).
  6. Print the value returned by hcf.
  7. Stop.

Program

The constructor sets a and b to 0, accept() takes the input, hcf() finds the GCD by calling itself, and display() checks the input and prints the result.

import java.util.Scanner;
public class GCD
{
    int a, b;

    GCD()
    {
        a = 0;
        b = 0;
    }

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        a = sc.nextInt();
        System.out.print("Enter the second number: ");
        b = sc.nextInt();
    }

    int hcf(int x, int y)
    {
        if (y == 0)
            return x;
        return hcf(y, x % y);
    }

    void display()
    {
        if (a <= 0 || b <= 0)
            System.out.println("Invalid input. Enter positive numbers only.");
        else
            System.out.println("GCD of " + a + " and " + b + " = " + hcf(a, b));
    }

    public static void main(String args[])
    {
        GCD ob = new GCD();
        ob.accept();
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
aintData member that stores the first number
bintData member that stores the second number
scScannerObject used to read the numbers from the keyboard
xintParameter of hcf(): the larger (or current first) number in each call
yintParameter of hcf(): the current second number; the recursion stops when it becomes 0
obGCDObject of the class created in main
argsString[]Command line arguments of main

Sample input and output

Sample 1

Enter the first number: 48
Enter the second number: 18
GCD of 48 and 18 = 6

Sample 2

Enter the first number: 17
Enter the second number: 5
GCD of 17 and 5 = 1

Sample 3

Enter the first number: 0
Enter the second number: 5
Invalid input. Enter positive numbers only.

One value per line

Dry run

Take $a = 48$ and $b = 18$. The call hcf(48, 18) works as follows.

Callxyy == 0?x % yNext call or result
14818No12hcf(18, 12)
21812No6hcf(12, 6)
3126No0hcf(6, 0)
460Yes-returns 6

The value 6 is returned back through calls 3, 2 and 1, so the output is GCD of 48 and 18 = 6, the same as the real run above.

Variations

  1. GCD and LCM "Write a program to input two numbers and find their GCD and LCM."
  2. Co-prime numbers "Input two numbers and check whether they are co-prime, that is, their GCD is 1."
  3. Lowest terms "Input the numerator and denominator of a fraction and reduce it to its lowest terms using the GCD."