1// gcd of two numbers in java
2import java.util.Scanner;
3public class GCDOfTwoNumbers
4{
5 public static void main(String[] args)
6 {
7 int a, b;
8 Scanner sc = new Scanner(System.in);
9 System.out.print("Please enter first number: ");
10 a = sc.nextInt();
11 System.out.print("Please enter second number: ");
12 b = sc.nextInt();
13 while(a != b)
14 {
15 if(a > b)
16 {
17 a = a - b;
18 }
19 else
20 {
21 b = b - a;
22 }
23 }
24 System.out.println("GCD of two numbers in java: " + b);
25 sc.close();
26 }
27}
1public class GCD {
2
3 public static void main(String[] args) {
4
5 int n1 = 81, n2 = 153, gcd = 1;
6
7 for(int i = 1; i <= n1 && i <= n2; ++i)
8 {
9 // Checks if i is factor of both integers
10 if(n1 % i==0 && n2 % i==0)
11 gcd = i;
12 }
13
14 System.out.printf("G.C.D of %d and %d is %d", n1, n2, gcd);
15 }
16}