#include<iostream>
using namespace std;
// This program will first input 5 positive integers from
// the user
// Then it will compute the "greatest common denominator"
// by first finding the largest number in the array
// and then trying integers (starting from the largest
// number in the array and counting down) and seeing if
// any of the numbers in the array have a remainder when
// dividing by each number. The first number which gives
// no remainder will be the "greatest common denominator".
// The smallest possible greatest common denominator is
// the number 1.
// Precondition: input array of positive integers and size
// of that array (which will always be 5 for
// this program)
// Postcondition: returns largest number in the array
int max(int array[], int array_size);
int main()
{
int inputNums[5], maxNum, gcd;
//gcd stands for greatest common denominator -- not the best variable name
// Get 5 positive integers from the user
cout << "Insert 5 positive integers: ";
for(int i=0; i<5; i++)
{
cin >> inputNums[i];
}
// Get max number in inputNums
maxNum=max(inputNums,5);
// Try integers to see what evenly divides all
// integers in the array
int tryNum=maxNum;
bool keepLooping=true;
while(keepLooping)
{ // if we haven't found a divisor yet
keepLooping=false; // start by assuming the current number
// tryNum will evenly divide all array
// numbers; if we discover this assumption
// is not true, we will set keepLooping=true
for(int i=0; i<5; i++)
{
if(inputNums[i]%tryNum!=0) // if tryNum does not evenly divide ith array value
keepLooping=true;
}
if(keepLooping) // before we do our next loop, decrease tryNum
tryNum--;
}
gcd=tryNum;
// Report greatest common denominator
cout << "The greatest common denominator is " << gcd << endl;
return 0;
}
// Find max number in the array
int max(int array[], int array_size)
{
int max_num=0;
for(int i=0; i<5; i++)
if(array[i]>max_num)
max_num=array[i];
}