Tuesday, November 4, 2014

Filled Under: , , , ,

Program to find H.C.F. of two numbers using BufferedReader.

Share

Code Box
--------------------------------------------

/*Program to find H.C.F. of two numbers */

import java.io.*;

class hcfb
{

public static void main(String x[]) throws IOException
{
int num1,num2,remainder,dividend,divisor;

BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n---------------H.C.F. Calculator --------------------");
System.out.print("\nEnter two number: ");


System.out.print("\nEnter First number: ");
num1=Integer.parseInt(obj.readLine());

System.out.print("\nEnter Second number: ");
num2=Integer.parseInt(obj.readLine());

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 --------------------");


}
}

0 comments:

Post a Comment