GCD Checking - using Division Method
For Example : We have accepted two number i.e. 24 & 18. In this case we follow Successive Division Method.
18 | 24 | 1
18
______
6 | 18 | 3
18
______
0
The Final Result of GCD Will be : 6
import java.util.*;
class GCD
{
int check_GCD(int x, int y)//Check GCD using Division Method
{
int r;
while(y > 0)
{
r = x % y;
x = y;
y = r;
}
return(x);
}
public static void main(String args[])
{
int a,b,n;
Scanner sc=new Scanner(System.in);
GCD ob=new GCD();
System.out.println("Enter Two Number for GCD = ");
a=sc.nextInt();
b=sc.nextInt();
n=ob.check_GCD(a,b);
System.out.println("GCD = "+n);
}
}
No comments