1int gcdByBruteForce(int n1, int n2) {
2 int gcd = 1;
3 for (int i = 1; i <= n1 && i <= n2; i++) {
4 if (n1 % i == 0 && n2 % i == 0) {
5 gcd = i;
6 }
7 }
8 return gcd;
9}
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}
1import java.util.Scanner;
2public class GCDExample3 {
3
4 public static void main(String[] args) {
5
6 int num1, num2;
7
8 //Reading the input numbers
9 Scanner scanner = new Scanner(System.in);
10 System.out.print("Enter first number:");
11 num1 = (int)scanner.nextInt();
12
13 System.out.print("Enter second number:");
14 num2 = (int)scanner.nextInt();
15
16 //closing the scanner to avoid memory leaks
17 scanner.close();
18 while (num1 != num2) {
19 if(num1 > num2)
20 num1 = num1 - num2;
21 else
22 num2 = num2 - num1;
23 }
24
25 //displaying the result
26 System.out.printf("GCD of given numbers is: %d", num2);
27 }