In this tutorial, we are going to learn writing java program to calculate the Highest Common Factor of two numbers. Our program will take two numbers as the inputs given by the user and returns the h.c.f. of given numbers.

**For example, **for the inputs of the two numbers 4 and 6. Our program will return ‘2’ as an output.

## What is H.C.F. or G.C.D.?

The Highest Common Factor (HCF), also known as the Greatest Common Divisor (GCD), is the largest number that divides two or more integers without leaving a remainder. It’s a fundamental concept in number theory and plays a key role in various mathematical and computational fields, such as simplifying fractions, least common multiple calculation, and cryptography.

### Examples:

**Numbers:**36 and 48**Prime Factorization**:- 36=22×3236=22×32
- 48=24×348=24×3

**Common Prime Factors**: The common prime factors are 22 and 33, with the lowest powers being 2222 and 33.**HCF**: 22×3=4×3=1222×3=4×3=12

## Program 1: Calculate HCF/GCD of two Numbers Java

In the below program we are taking two numbers as input from the user. After taking inputs, we are applying logics to find the HCF. WE are using for loop to iterate through possible factors, determining the greatest common divisor. The output for the input numbers 6 and 2 was correctly calculated as 2.

```
import java.util.*;
public class Main
{
public static void main(String[] args) {
double num1,num2,gcd=0;
System.out.println("Java Program to calculate HCF/GCD " );
Scanner sc = new Scanner(System.in);
System.out.println("Please give first number");
num1= sc.nextDouble();
System.out.println("Please give second number");
num2= sc.nextDouble();
for(int i=1; i <= num1 && i <= num2; ++i)
{
if(num1%i==0 && num2%i==0)
gcd = i;
}
System.out.println("G.C.D of number "+num1+" and "+num2+" = " +gcd);
}
}
```

**Output**

```
Java Program to calculate HCF/GCD
Please give first number
6
Please give second number
2
G.C.D of number 6.0 and 2.0 = 2.0
```

**Explanations**

For inputs 6 and 2, the program is finding the highest factor that is common in both 6 and 2.

The factor of 6 is 1,2,3 and the factor of 2 is 1,2. The highest factor common in

both is 2.

## Program 2: Calculate HCF/GCD of two Numbers using Java 8

In the below program we are using Java 8 streams. We have created a `calculateGCD`

method which is using stream to iterate through the range of numbers and filter for common divisors. The `max`

method is using to find the greatest common divisor. The output for the input numbers 45 and 5 was correctly computed as 5.

```
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please give first number:");
int num1 = scanner.nextInt();
System.out.println("Please give second number:");
int num2 = scanner.nextInt();
int gcd = calculateGCD(num1, num2);
System.out.println("HCF of " + num1 + " and " + num2 + " is: " + gcd);
}
private static int calculateGCD(int a, int b) {
return IntStream.rangeClosed(1, Math.min(a, b))
.filter(i -> a % i == 0 && b % i == 0)
.max()
.orElse(1);
}
}
```

**Output**

```
Please give first number:
45
Please give second number:
5
HCF of 45 and 5 is: 5
```

## Conclusion

In the above tutorial you have learnt Writing Java programs to calculate the Highest Common Factor (HCF) or Greatest Common Divisor (GCD) of two given numbers. The HCF represents the largest number that can exactly divide both input numbers.

In the first program we have taken input from the user to find the HCF. And then using for loop we have calculated the gcd of the two input number.

And in second program, we are also taking the input from the user but applying java 8 stream api to calculate the gcd.

Readers now have two different methods to calculate the HCF of two numbers in Java. Whether using a traditional iterative approach or leveraging the functional capabilities of Java 8 streams, you have the flexibility to choose the method that best fits your coding preferences and requirements.

Happy coding!