Hello Shouters !! Today we will understand the Java program to Find HCF of two numbers.

We will use two methods to find the HCF of Two Numbers, the first one will be without taking the function and the other one will be using the functions.

## What is HCF?

HCF stands for Highest Common Factor and is the highest common divisor between two numbers. In simple terms, if we have two numbers then the biggest number that can divide both the numbers completely is know as the HCF of the number.

## 1. First Method- Without Function

In this method, we will only take two numbers and will do the manipulation of the data to get the HCF.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Java Program to Find the HCF of two numbers import java.util.*; public class HCF{ public static void main(String[] args) { int n1, n2; //Getting the input numbers from users Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number:"); n1 = (int)scanner.nextInt(); System.out.print("Enter the second number:"); n2 = (int)scanner.nextInt(); while (n1 != n2) { if(n1 > n2) n1 = n1 - n2; else n2 = n2 - n1; } //displaying the HCF of two numbers System.out.print("HCF of the given numbers is: %d", n2); } |

**Output – ** Enter the first number 90 Enter the Second Number 75 HCF of the given numbers is 15

## 2. Second Method- Using Functions

In this method, we will be using a function to calculate the HCF of the two numbers.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.*; public class HCF{ public static void main(String[] args) { int n1, n2; //Taking the input numbers from users Scanner scanner = new Scanner(System.in); System.out.print("Enter first number:"); n1 = (int)scanner.nextInt(); System.out.print("Enter second number:"); n2 = (int)scanner.nextInt(); int hcf= HCF_func(int n1, int n2); //displaying the result System.out.printf("GCD of given numbers is: %d", hcf); } int HCF_func(int n1, int n2) { while (n1 != n2) { if(n1 > n2) n1 = n1 - n2; else n2 = n2 - n1; } return n1; } |

**Output –** Enter the first number 90 Enter the Second Number 75 HCF of the given numbers is 15

It is very good to know two Java program to find the HCF of two numbers for placement interviews and general knowledge.

We daily add Multiple Choice Questions for placement preparation questions and technical interview preparation on the Instagram page. Check the Instagram account: https://www.instagram.com/shoutcoders/

## Frequently Asked Questions-

**What is HCF of two numbers?**

HCF stands for highest Common Factor and is the highest common divisor in the two numbers.

**Is GCD and HCF different?**

No, GCD stands for Greatest Common divisor and HCF stands for Highest Common Factor both are same and can be find using same program.

**What is the HCF of two prime numbers?**

Prime Numbers are only divisible by 1 and the number itself. So, the HCF of two prime numbers is always 1.

**Is the program important from placement perspective?**

Yes, it is very important from placement view and asked in many major companies.

## Recommended Posts –

- Introduction and Installation of Java on Windows
- Program to add two numbers in Java
- Java Program to Calculate the Factorial of a number.
- Java Program to Swap two numbers
- Java Program to Check Whether a Number is an Even or Odd

canadian online pharmacies legitimate canada discount drug

cheap prescription drugs medical pharmacies

walmart pharmacy online https://ukcanadianpharmacy.com/

Like!! Thank you for publishing this awesome article.

Please let me know if you’re looking for a author for your blog.

You have some really great posts and I think I would be a good asset.

If you ever want to take some of the load off, I’d

really like to write some content for your blog in exchange for a link

back to mine. Please blast me an email if interested. Thank

you!

Here is my blog :: CBD for sale