Skip to content

Commit 3adb0e7

Browse files
authored
Merge pull request #1 from RG0512/RG0512-patch-1
Create arrayGCD.java
2 parents b1977a2 + 7e039c2 commit 3adb0e7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

raj/arrayGCD.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Java program to find GCD of two or
2+
// more numbers
3+
4+
public class GCD {
5+
// Function to return gcd of a and b
6+
static int gcd(int a, int b)
7+
{
8+
if (a == 0)
9+
return b;
10+
return gcd(b % a, a);
11+
}
12+
13+
// Function to find gcd of array of
14+
// numbers
15+
static int findGCD(int arr[], int n)
16+
{
17+
int result = arr[0];
18+
for (int i = 1; i < n; i++)
19+
result = gcd(arr[i], result);
20+
21+
return result;
22+
}
23+
24+
public static void main(String[] args)
25+
{
26+
int arr[] = { 2, 4, 6, 8, 16 };
27+
int n = arr.length;
28+
System.out.println(findGCD(arr, n));
29+
}
30+
}
31+
32+
// This code is contributed by Saket Kumar

0 commit comments

Comments
 (0)