Code Box
---------------------------------------
/*Program to find H.C.F. of two numbers */
class hcf
{
public static void main(String x[])
{
int num1,num2,remainder,dividend,divisor;
num1=Integer.parseInt(x[0]);
num2=Integer.parseInt(x[1]);
System.out.print("\n---------------H.C.F. Calculator --------------------");
System.out.print("\nEntered numbers are: ");
System.out.print("\nFirst number: "+num1);
System.out.print("\nSecond number: "+num2);
if(num1>num2) //Checker for greater number
{
remainder=num1%num2;
dividend=num2;
}
else{
remainder=num2%num1;
dividend=num1;
}
//If remainder is zero
if(remainder==0)
{
System.out.print("\nH.C.F. of "+num1+" & "+num2+" is : "+dividend);
}
else
{
divisor=remainder;
while(remainder !=0)
{
remainder=dividend%divisor;
dividend=divisor;
divisor=remainder;
}
System.out.print("\nH.C.F. of "+num1+" & "+num2+" is : "+dividend);
}
System.out.print("\n---------------H.C.F. Calculator --------------------");
}
}
Nice article for find hcf for two number .one more way to find hfc and lcm visit HCF and LCM of two number
ReplyDelete