Skip to content

Commit 99e6b6a

Browse files
GCD of two numbers in JAVA
1 parent bc304c5 commit 99e6b6a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

gcd/gcd.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Java program to find factorial of given number
2+
class gcd
3+
{
4+
// Recursive function to return gcd of a and b
5+
static int gcd(int a, int b)
6+
{
7+
// Everything divides 0
8+
if (a == 0 || b == 0)
9+
return 0;
10+
11+
// base case
12+
if (a == b)
13+
return a;
14+
15+
// a is greater
16+
if (a > b)
17+
return gcd(a-b, b);
18+
return gcd(a, b-a);
19+
}
20+
21+
// Driver method
22+
public static void main(String[] args)
23+
{
24+
int a = 98, b = 56;
25+
System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));
26+
}
27+
}

0 commit comments

Comments
 (0)