-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathQ18GCD.cpp
More file actions
42 lines (40 loc) · 695 Bytes
/
Q18GCD.cpp
File metadata and controls
42 lines (40 loc) · 695 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
// C++ program to find GCD of n numbers
#include<iostream>
using namespace std;
// Function to return gcd of a and b
int gcd(int a,int b)
{
if (a==0)
return b;
return gcd(b%a,a);
}
// Function to find gcd
int findGCD(int arr[],int n)
{
int result=arr[0];
for(int i=1;i<n;i++)
{
result=gcd(arr[i],result);
if(result == 1)
{
return 1;
}
}
return result;
}
// Driver code
int main()
{
int n;
cout<<"Enter size of array:";
cin>>n;
int arr[n];
cout<<"Enter numbers: ";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"GCD of numbers: ";
cout<<findGCD(arr,n);
return 0;
}