-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCD.java
More file actions
36 lines (33 loc) · 1.1 KB
/
GCD.java
File metadata and controls
36 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
System.out.println("Welcome to GCD!"); // Greatest Common Divisor
System.out.print("Enter first number: ");
int first = input.nextInt();
System.out.print("Enter second number: ");
int second = input.nextInt();
int gcd = gcd(first, second);
System.out.println("GCD of the number is: " + gcd); // Main result aita run korb
}
}
public static int gcd(int num1, int num2) { //GCD int diye declear korci
int gcd = 1;
int i = 2;
int least = least(num1, num2);
while (i <= least) {
if (num1 % i == 0 && num2 % i == 0) {
gcd = i;
}
i++;
}
return gcd;
}
public static int least(int num1, int num2) { // least diye int ekta declear korci
if (num1 < num2) {
return num1;
} else {
return num2;
}
}
}