# Java Program to Find GCD of Two Numbers

In this section, we have covered different logics in Java programs to find GCD of two numbers.

Greatest Common Divisor: It is the highest number that completely divides two or more numbers. It is abbreviated for GCD. It is also known as the Greatest Common Factor (GCF) and the Highest Common Factor (HCF). It is used to simplify the fractions.

### How to Find the Greatest Common Factor

• Write all the factors of each number.
• Select the common factors.
• Select the greatest number, as GCF.

Example: Find the GCF of 12 and 8.

Solution:

Factors of 12: 1, 2, 3, 4, 6, 12

Factors of 8: 1, 2, 4, 8

Common Factors: 1, 2, 4

Greatest Common Factor: 4

Hence, the GCF of 12 and 8 is 4.

### Algorithm to Find GCD

• Declare two variables, say x and y.
• Run a loop for x and y from 1 to max of x and y.
• Check that the number divides both (x and y) numbers completely or not. If divides completely store it in a variable.
• Divide the stored number.

In Java, we can use the following ways to find the GCD of two numbers:

• Using Java for loop
• Using while loop
• Using User-Defined Method
• Using the Euclidean Algorithm
• Using Modulo Operator

## Using Java for loop

In the following program, we have initialized two numbers x=12 and y=8. After that, we have used a for loop that runs from 1 to the smallest of both numbers. It executes until the condition i <= x && i <= y returns true. Inside the for loop, we have also used if statement that tests the condition (x%i==0 && y%i==0) and returns true if both conditions are satisfied. At last, we have store the value of i in the variable gcd and print the same gcd variable.

FindGCDExample1.java

1. public class FindGCDExample1
2. {
3. public static void main(String[] args)
4. {
5. //x and y are the numbers to find the GCF
6. int x = 12, y = 8, gcd = 1;
7. //running loop form 1 to the smallest of both numbers
8. for(int i = 1; i <= x && i <= y; i++)
9. {
10. //returns true if both conditions are satisfied
11. if(x%i==0 && y%i==0)
12. //storing the variable i in the variable gcd
13. gcd = i;
14. }
15. //prints the gcd
16. System.out.printf(“GCD of %d and %d is: %d”, x, y, gcd);
17. }
18. }

Output:

```GCD of 12 and 8 is: 4
```

## Using Java while Loop

In the following example, we have used while loop to test the condition. The loop executes until the condition n1!=n2 becomes false.

FindGCDExample2.java

1. public class FindGCDExample2
2. {
3. public static void main(String[] args)
4. {
5. int n1=50, n2=60;
6. while(n1!=n2)
7. {
8. if(n1>n2)
9. n1=n1-n2;
10. else
11. n2=n2-n1;
12. }
13. System.out.printf(“GCD of n1 and n2 is: ” +n2);
14. }
15. }

Output:

```GCD of n1 and n2 is: 10
```

In the above program, we can replace the while loop with the following logic that gives the same output.

FindGCDExample3.java

1. public class FindGCDExample3
2. {
3. public static void main(String[] args)
4. {
5. int Num1=12, Num2=8, Temp, GCD=0;
6. while(Num2 != 0)
7. {
8. Temp = Num2;
9. Num2 = Num1 % Num2;
10. Num1 = Temp;
11. }
12. GCD = Num1;
13. System.out.println(“\n GCD =  ” + GCD);
14. }
15. }

Output:

```GCD =  4
```

## Using User-Defined Method

In the following program, we have defined a method named findGCD(). It contains the logic to find the GCD of two numbers. We have parsed two parameters a and b of type int.

FindGCDExample4.java

1. import java.util.Scanner;
2. public class FindGCDExample4
3. {
4. //private static Scanner sc;
5. public static void main(String[] args)
6. {
7. int a, b, gcd = 0;
8. Scanner sc = new Scanner(System.in);
9. System.out.print(“Enter the First Number: “);
10. a = sc.nextInt();
11. System.out.print(“Enter the Second Number: “);
12. b = sc.nextInt();
13. gcd = findGCD(a, b);
14. System.out.println(“GCD of ” + a + ” and ” + b + ” =  ” + gcd);
15. }
16. public static int findGCD(int a, int b)
17. {
18. while(b != 0)
19. {
20. if(a > b)
21. {
22. a = a – b;
23. }
24. else
25. {
26. b = b – a;
27. }
28. }
29. return a;
30. }
31. }

Output:

```Enter the First Number: 75
Enter the Second Number: 125
GCD of 75 and 125 = 25
```

## Using the Euclidean Algorithm

The Euclidean Algorithm is an efficient method to compute GCD of two numbers. It is also known as Euclid’s Algorithm. The algorithm states that:

• If A=0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can exit from the algorithm.
• If B=0 then GCD(A,B)=A, since the GCD(A,0)=A, and we can exit from the algorithm.
• Write A in the quotient that we get from (A=B*Q+R).

Let’s implement the above logic in a Java program.

FindGCDExample5.java

1. import java.util.Scanner;
2. public class FindGCDExample5
3. {
4. public static void main(String args[])
5. {
6. Scanner sc = new Scanner(System.in);
7. System.out.println(“Enter the two numbers: “);
8. int x = sc.nextInt();
9. int y = sc.nextInt();
10. System.out.println(“The GCD of two numbers is: ” + findGCD(x,y));
11. }
12. static int findGCD(int x, int y)
13. {
14. int r=0, a, b;
15. a = (x > y) ? x : y; // a is greater number
16. b = (x < y) ? x : y; // b is smaller number
17. r = b;
18. while(a % b != 0)
19. {
20. r = a % b;
21. a = b;
22. b = r;
23. }
24. return r;
25. }
26. }

Output:

```Enter the two numbers:
11
33
The GCD of two numbers is: 11
```

## Using Modulo Operator

In the following program, we have defined a recursive function named findGCD(). It parses two parameters a and b of type int. If the second number (b) is equal to 0, the method returns a as GCD, else returns a%b.

FindGCDExample6.java

1. public class FindGCDExample6
2. {
3. public static void main(String[] args)
4. {
5. int a = 112, b = 543;
6. System.out.println(“GCD of ” + a +” and ” + b + ” is ” + findGCD(a, b));
7. }
8. //recursive function to return gcd of a and b
9. static int findGCD(int a, int b)
10. {
11. if (b == 0)
12. return a;
13. return findGCD(b, a % b);
14. }
15. }

Output:

```GCD of 112 and 543 is 1
```

Let’s see another logic to find the GCD of two numbers.

FindGCDExample7.java

1. public class FindGCDExample7
2. {
3. public static void main(String[] args)
4. {
5. int a = 54, b = 24;
6. System.out.println(“GCD of ” + a +” and ” + b + ” is ” + gcd(a, b));
7. }
8. //recursive function to return gcd of a and b
9. static int gcd(int a, int b)
10. {
11. // Everything divides 0
12. if (a == 0)
13. return b;
14. if (b == 0)
15. return a;
16. if (a == b)
17. return a;
18. if (a > b)
19. return gcd(a-b, b);
20. return gcd(a, b-a);
21. }
22. }

Output:

`GCD of 54 and 24 is 6`