Java Program to find the Common factor of two number

Java Program to find the Common factor of two number

Finding common factor of two number is also know as finding the GCD or HCF.

GCD is a mathematical term stands for greatest common divisor (GCD). GCD is a largest non-zero positive integer of two or more integers that divides each of the integers.

HCF and GCD both are same. HCF stands for Highest common factor, means common factor between the two intergers.

Program 1 : Java Program to find highest common factor

[java]
public class Main
{
public static void main(String[] args)
{
int num1=50, num2=60;
while(num1!=num2)
{
if(num1>num2){
num1=num1-num2;
}
else{
num2=num2-num1;
}
}
System.out.printf(“GCD of num1 and num2 is: ” +num2);
}
}
[/java]

Output

Program 2 : Java Program to find the Common factor of two number

[java]
public class Main
{
public static void main(String …a)
{
int num1=50, num2=10, temp, GCD=0;
while(num2 != 0)
{
temp = num2;
num2 = num1 % num2;
num1 = temp;
}
GCD = num1;
System.out.println(“\n GCD of num1 and num2 = ” + GCD);
}
}
[/java]

Output