GCD OF TWO NUMBERS.
This is simple java program that will calculate GCD of two given numbers. In below code function check_gcd() is used and it takes two arguments which are two input numbers and calculate GCD.
JAVA CODE : )
public class GCD
{
int i,gcd;
void check_gcd(int x,int y)
{
for(i=1;i<=x && i<=y;++i)
{
if(x%i==0 && y%i==0)
gcd=i;
}
System.out.print("\nGCD of "+x+" and "+y+" is: "+gcd);
}
public static void main(String[] args)
{
GCD g = new GCD();
g.check_gcd(300,900);
g.check_gcd(625,825);
g.check_gcd(789,908);
g.check_gcd(3333,99);
}
}
0 Comments