Java program to find HCF of two numbers – The below given Java programs explains the process of evaluating the Highest Common Factor(HCF) between two given numbers.
The methods used to find the HCF of two numbers is Java Programming is as follows:
- Using Command Line Arguments
- Using Static method
- Using Recursion
In the end, we will discuss how to find the HCF of n numbers. We do also add compiler for each and every program at the end of the article, with proper examples and sample outputs.
What Is HCF?
HCF or Highest Common Factor is the greatest common divisor between two or more given numbers.
For example:
Let there be two arbitrary numbers such as 75 and 90.
75 = 3 * 5 * 5
90 = 2 * 3 * 3 * 5
Common Divisor = 3 * 5 = 15
Here, the HCF of the three given numbers would be 15 since it divides every given number without leaving a fraction behind.
HCF Of Two Numbers – Command Line Arguments
1) The values we will pass at run-time are called command line arguments.
2) n1=Integer.parseInt(arg[0]); here reading the first value from string array which is at index “0”, and converting the string value into an integer using parseInt() method.
n2=Integer.parseInt(arg[1]); here reading the 2nd value from string which is at index “1” and converting string value into integer using parseInt() method. Here Integer is “wrapper” class.
3) The while loop iterates until n2>0 is false temp assigned to n2, n2 assigned to the remainder of n1/n2, n1 assigned to n1. If the condition fails then it prints the HCF of two numbers is n1.
4) In our example, we are passing 15, 35 as command line arguments, and converted as integers and store the values at n1=15, n2=35. while loop checks the condition 35>0 is true , temp=35, n2=5, n1=35, 5>o true,temp=5,n2=0,n1=5. For n2=0 condition fails and prints the HCF is 5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Hcf { public static void main(String arg[]) { int n1=Integer.parseInt(arg[0]); int n2=Integer.parseInt(arg[1]); int temp; while (n2 > 0) { temp = n2; n2 = n1% n2; n1 = temp; } System.out.println("hcf="+n1); } } |
Output:
1 2 |
>java Hcf 15 35 hcf=5 |
Using Static Method
- Calls the static hcfCal() method in the main method, the method executes the code and prints the value.
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 |
import java.util.Scanner; class Hcf { public static void main(String arg[]) { long n1,n2; Scanner sc=new Scanner(System.in); System.out.println("Enter number 1"); n1=sc.nextLong(); System.out.println("Enter number 2"); n2=sc.nextLong(); long result=hcfCal(n1,n2); System.out.println("Hcf of two numbers = "+result); } static long hcfCal(long a,long b) { long temp; while (b > 0) { temp = b; b = a % b; a = temp; } return b; } } |
Output:
1 2 3 4 5 |
enter number 1 25 enter number 2 50 Hcf of two numbers = 25 |
Using Recursion
1) In this program, the method highest(int a, int b) calls itself as highest(a,b).
2) Using scanner object, read the values and store in the variables n1, n2. Using Hcf class object call the method highest(n1,n2), the method starts the execution and calls itself, it repeats until the condition if(a!=b) is false.
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 29 |
import java.util.Scanner; class Hcf { int temp; int highest(int a,int b) { if(a!=b) { if(a>b) a=a-b; else b=b-a; return highest(a,b); } return a; } public static void main(String arg[]) { Hcf h=new Hcf( ); int n1,n2; Scanner sc=new Scanner(System.in); System.out.println("Enter first number"); n1=sc.nextInt(); System.out.println("Enter second number"); n2=sc.nextInt(); System.out.println("Hcf of two numbers is="+h.highest(n1,n2)); } } |
Output:
1 2 3 4 5 |
Enter first number 25 Enter second number 3 Hcf of two numbers is=1 |
Finding HCF Of n numbers
1) We are finding the HCF of n numbers by passing each element of an array to the function hcf(result, input[i]) using for loop with the structure for(int i = 1; i < input.length; i++).
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 29 30 31 32 33 34 35 |
import java.util.Scanner; class Hcf { public static void main(String arg[]) { { Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); int n=sc.nextInt(); long input[]=new long[n]; System.out.println("Enter "+ n+" numbers"); for(int i=0;i<n;i++) { input[i]=sc.nextLong(); } long result = input[0]; for(int i = 1; i < input.length; i++) { result= hcf(result, input[i]); } System.out.println("HCF="+result); } } static long hcf(long a,long b) { while (b > 0) { long temp = b; b = a % b; a = temp; } return a; } } |
Output:
1 2 3 4 5 6 |
Enter a number 2 Enter 2 numbers 4 16 HCF=4 |