2017-12-24 01:30:49 +08:00
|
|
|
//This program aims at calculating the GCD of n numbers by division method
|
|
|
|
#include <iostream>
|
|
|
|
using namepsace std;
|
|
|
|
int main()
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "Enter value of n:" << endl;
|
2017-12-24 01:30:49 +08:00
|
|
|
cin >> n;
|
|
|
|
int a[n];
|
2019-08-21 10:10:08 +08:00
|
|
|
int i, j, gcd;
|
2017-12-24 01:30:49 +08:00
|
|
|
cout << "Enter the n numbers:" << endl;
|
2019-08-21 10:10:08 +08:00
|
|
|
for (i = 0; i < n; i++)
|
2017-12-24 01:30:49 +08:00
|
|
|
cin >> a[i];
|
2019-08-21 10:10:08 +08:00
|
|
|
j = 1; //to access all elements of the array starting from 1
|
|
|
|
gcd = a[0];
|
|
|
|
while (j < n)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (a[j] % gcd == 0) //value of gcd is as needed so far
|
|
|
|
j++; //so we check for next element
|
2017-12-24 01:30:49 +08:00
|
|
|
else
|
2019-08-21 10:10:08 +08:00
|
|
|
gcd = a[j] % gcd; //calculating GCD by division method
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
cout << "GCD of entered n numbers:" << gcd;
|
|
|
|
}
|