forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd_of_array.cpp
More file actions
44 lines (39 loc) · 745 Bytes
/
gcd_of_array.cpp
File metadata and controls
44 lines (39 loc) · 745 Bytes
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
//
// C++ program to find GCD of an array of integers
//
// The All ▲lgorithms Project
//
// https://allalgorithms.com/math
// https://github.com/allalgorithms/cpp
//
// Contributed by: Bharat Reddy
// Github: @Bharat-Reddy
//
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int findGCD(int arr[], int n)
{
int result = arr[0];
for (int i = 1; i < n; i++)
result = gcd(arr[i], result);
return result;
}
int main()
{
int n;
cout<<"Enter size of array : ";
cin>>n;
int a[n];
cout<<"Enter elements of array"<<endl;
int i;
for(i=0;i<n;i++)
cin>>a[i];
cout << findGCD(a, n) << endl;
return 0;
}