Saturday, 28 November 2015

PROGRAM TO FIND GREATEST COMMOM FATOR OF TWO RANDOM NUMBERS

//program to find greatest common factor of two random numbers
class Gcd
{
public static void main(String args[])
{
int gcd=1,x=(int)(Math.random()*10);
int y=(int)(Math.random()*10);
int max;
if(x>y){max=x;}
else{max=y;}
for(int i=1;i<=max;i++)
{
if(x%i==0 && y%i==0)
{
gcd=i;
}
}
System.out.println(“Greatest common fator of “+x+” and “+y+” is “+gcd);
}
}

No comments:

Post a Comment