# 1. Introduction

In this tutorial, we’ll explore a Java program designed to calculate the Greatest Common Divisor (GCD) of two numbers. The GCD of two integers is the largest positive integer that divides both numbers without leaving a remainder. For example, the GCD of 8 and 12 is 4.

# 2. Program Steps

1. Define a class named *GCD_Calculator*.

2. Define the *main* method inside this class.

3. Inside the *main* method, initialize two integer variables *num1* and *num2* with the numbers for which you want to find the GCD.

4. Call the *findGCD* method, passing *num1* and *num2* as parameters.

5. Print the calculated GCD.

# 3. Code Program

```
public class GCD_Calculator { // 1. Define the class
// Method to find GCD of two numbers using Euclidean algorithm
static int findGCD(int a, int b) {
if (b == 0)
return a;
else
return findGCD(b, a % b);
}
public static void main(String[] args) { // 2. Define the main method
int num1 = 56, num2 = 98; // 3. Initialize num1 and num2
// 4. Call the findGCD method and store the result in variable 'gcd'
int gcd = findGCD(num1, num2);
// 5. Print the result
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);
}
}
```

### Output:

GCD of 56 and 98 is: 14

# 4. Step By Step Explanation

– *Step 1:* A class named *GCD_Calculator* is defined.

– *Step 2:* The *main* method is defined within the class, acting as the entry point for the Java program.

– *Step 3:* Two integer variables *num1* and *num2* are initialized with the values *56* and *98* respectively, for which we wish to calculate the GCD.

– *Step 4:* The method *findGCD* is called with *num1* and *num2* as parameters. This method employs the Euclidean algorithm to calculate the GCD. If the second number *b* is *0*, the method returns the first number *a*. Otherwise, it recursively calls itself with *b* and the remainder of *a* divided by *b* as the parameters.

– *Step 5:* The program outputs the calculated GCD of *56* and *98*, which is *14*, to the console.