-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCD.java
68 lines (59 loc) · 1.39 KB
/
GCD.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.sbiswas001.twelveproject;
import java.util.Scanner;
/**
* This class calculates the greatest common divisor of two
* numbers.
* @author Sayan Biswas
* @version 23.04.2022
*/
public class GCD {
/**
* Stores first number
*/
private int num1;
/**
* Stores second number
*/
private int num2;
/**
* Initialises instance variables
*/
private GCD() {
num1 = 0;
num2 = 0;
}
/**
* Inputs two numbers from user
*/
private void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
num1 = Integer.parseInt(sc.next());
System.out.print("Enter second number: ");
num2 = Integer.parseInt(sc.next());
}
/**
* Calculates the greatest common divisor of two numbers.
* @param a First number
* @param b Second number
* @return GCD
*/
private int setGCD(int a, int b) {
for(int i = Math.max(a,b); i > 1; i--) {
if(a % i == 0 && b % i == 0) {
return i;
}
}
return 1;
}
/**
* Calls other methods
* @param args Arguments passed to main method
*/
public static void main(String[] args) {
GCD ob = new GCD();
ob.input();
System.out.println("GCD is " +
ob.setGCD(ob.num1, ob.num2));
}
}