-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind GCD or HCF of two numbers.java
More file actions
57 lines (45 loc) · 1.14 KB
/
Copy pathFind GCD or HCF of two numbers.java
File metadata and controls
57 lines (45 loc) · 1.14 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*Write a program that prints Java program to find GCD of two numbers
Input
Line 1: First Number,N1
Line 2: Second Number,N2
Output
Display 0 (zero) if either of the inputs is zero
Otherwise, display the GCD of two numbers,
knowing that if two numbers are equally print same
Constraints
0 ≤ N1,N2 < 10000
Example
Input:
96
84
Output:
12
Conditions
Available RAM: 512MB
Timeout: 1 seconds
The program has to read inputs from standard input
The program has to write the solution to standard output
The program must run in the test environment
*/
import java.util.Scanner;
public class MyClass
{
// Recursive function to return gcd of a and b
static int gcd(int a, int b)
{
if(a==0)
return b;
if (b == 0)
return a;
return gcd(b, a % b);
}
// Using Euclidean algorithm
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int N1 = scanner.nextInt();
int N2 = scanner.nextInt();
System.out.println(gcd(N1,N2));
in.close();
}
}