• Yogeeta Paryani

JAVA| Java program to calculate HCF of two numbers.

In this blog we're going to calculate HCF of two no. using java.

An H.C.F or Highest Common Factor, is the largest common factor of two or more values.

For eg. Factors of 9 and 6 are-

9 - 1,3,9

6 - 1,2,3,6

The common factors are 1, 3 and the highest common factor is 3.


1. Import Scanner library then create class, now under main class define two variables you want HCF for and also define variable i and hcf.

 import java.util.Scanner;
class Hcf
  public static void main(String args[])
     int n1,n2,i,hcf=0;

2. Now take the value of numbers from user.

Scanner sc=new Scanner(System.in);
     System.out.print("Enter first no.");
     System.out.print("Enter Second no.");

3. Create a loop from 1 to maximum of numbers.


4. Check if both are completely divided by same loop number, if yes, store it


5. Display the stored number is HCF

 System.out.println("HCF of given two no. is="+hcf);


Enter first no.9
Enter Second no.6
HCF of given two no. is=3

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor

Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

#blog #JAVA #HCF #learn #coding #programming

26 views0 comments

Recent Posts

See All

        Contact Us


  • LinkedIn
  • Facebook
  • Instagram

©2023 by Programmers Door