HCF or Highest Common Factor is the greatest number which divides each of the two or more numbers. HCF is also called the Greatest Common Measure (GCM) and Greatest Common Divisor(GCD).
import java.util.Scanner;
public class HCF {
public static void main(String[] args) {
int a, b, hcf=0, i, c=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any two number whose HCF you want to find : ");
a = sc.nextInt();
b = sc.nextInt();
c = a * b;
for(i=1;i<=c;i++){
if (a%i==0 && b%i==0){
hcf = i;
}
}
System.out.println("HCF IS : " + hcf);
}
}
Comments
Post a Comment