Write a program that finds out HCF of two numbers a and b.
What Is HCF?
HCF or Highest Common Factor is the greatest common divisor between two 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.
Solution of HCF:–
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { //your code here Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int temp=1; for(int i=1;i<=a;i++) { if(a%i==0 && b%i==0) { temp=i; } } System.out.println(temp); } }
Add a Comment