From 2fef097a189446d0eba2f6cd7db46a0e247dc375 Mon Sep 17 00:00:00 2001 From: srimanchakri <56732426+srimanchakri@users.noreply.github.com> Date: Fri, 18 Oct 2019 21:10:04 +0530 Subject: [PATCH] Create arrraygcd.cpp --- arrraygcd.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 arrraygcd.cpp diff --git a/arrraygcd.cpp b/arrraygcd.cpp new file mode 100644 index 0000000..1e9d441 --- /dev/null +++ b/arrraygcd.cpp @@ -0,0 +1,33 @@ + +// C++ program to find GCD of two or +// more numbers +#include +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 of array of +// numbers +int findGCD(int arr[], int n) +{ + int result = arr[0]; + for (int i = 1; i < n; i++) + result = gcd(arr[i], result); + + return result; +} + +// Driver code +int main() +{ + int arr[] = { 2, 4, 6, 8, 16 }; + int n = sizeof(arr) / sizeof(arr[0]); + cout << findGCD(arr, n) << endl; + return 0; +}