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}
1/* Function using "Euclidian Algorithm" to recursively find the
2greatest common divisor/factor (GCD/GCF) of 2 positive numbers*/
3const gcf = function (small, large) {
4 let r = large % small;
5 if (r == 0)
6 return small;
7 else
8 return gcf(r, small);
9}
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 }
1static int gcd(int a, int b)
2 {
3 if (b == 0)
4 return a;
5 return gcd(b, a % b);
6 }
7