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
Latest commit
36 lines (30 loc) · 620 Bytes
/
Copy pathgcd_of_array.cpp
File metadata and controls
36 lines (30 loc) · 620 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
// C++ program to find GCD of an array of integers
//Author Bharat Reddy
#include<bits/stdc++.h>
usingnamespacestd;
intgcd(int a, int b)
{
if (a == 0)
return b;
returngcd(b % a, a);
}
intfindGCD(int arr[], int n)
{
int result = arr[0];
for (int i = 1; i < n; i++)
result = gcd(arr[i], result);
return result;
}
intmain()
{
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;
return0;
}