Programming Examples

Java program to input two integers and find their Highest Common Factor


Write a program to input two integers and find their Highest Common Factor(H.C.F).

For Example,

INPUT: Enter 2 integers:

12

8

OUTPUT: H.C.F. = 4

Solution

import java.util.*;
class HCF
{
	public static void main(String arr[])
	{
		Scanner sc=new Scanner(System.in);
		int i,a,b,h=0;
		System.out.println("Enter 2 integers:");
		a=sc.nextInt();
		b=sc.nextInt();
		for(i=a;i>=1;i--)
		{
			if(a%i==0 && b%i==0)
			{
				h=i;
				break;
			}
		}
		System.out.println("H.C.F.="+h);
	}
}
Output

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad