-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreatestCommonDivisor.java
More file actions
42 lines (35 loc) · 1.13 KB
/
GreatestCommonDivisor.java
File metadata and controls
42 lines (35 loc) · 1.13 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
37
38
39
40
41
42
import java.util.Scanner;
public class GreatestCommonDivisor {
public static void main(String[] args) {
// -----------------------Method 1----------------------------
// System.out.println(getGreatestCommonDivisor(15,20));
// }public static int getGreatestCommonDivisor(int first, int second){
// if(first<10 || second<10){
// return -1;
// }
//
// while(first!=second){
//
// if(first>second){
// first=first-second;
// }
// else{
// second=second-first;
// }
// }
// return second;
// ------------------------Method 2---------------------------
Scanner scanner=new Scanner(System.in);
System.out.print("Enter two Positive integers: ");
int num1=scanner.nextInt();
int num2=scanner.nextInt();
scanner.close();
int greatestCommonDivisor=1;
for(int i=1; i<=num1 && i<=num2; i++) {
if(num1%i==0 && num2%i==0) {
greatestCommonDivisor=i;
}
}
System.out.println("Greatest Common Divisor of "+num1+" and "+num2+" = "+greatestCommonDivisor);
}
}