Euclidean GCD of multiple values

May 2, 2016 at 2:32am
Hi everyone,
I'm currently having trouble developing an algorithm to find the GCD of multiple values using the euclidean algorithm. Currently, my program works if only two values are entered by the user. But, how can I modify the algorithm below to calculate the GCD of more than two values? Thank you very much for taking the time to read this question and have a great day!

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Function Prototypes
int calcGCD(int a, int b);

int main()
{
	// Variable Declaration
	int a, b, result; 

	cout << "Author: Sydney Pun\n";
	cout << "Euclidean GCD Program" << endl;
	cout << endl;

	cout << "Enter two integers: "; 
	cin >> a >> b; 

	result = calcGCD(a, b);

	cout << "The GCD of the two integers is: " << result << endl;

	return 0;
}

int calcGCD(int a, int b)
{
	if ((a % b) == 0)
		return b;
	else
		return calcGCD(b, a % b);
}
May 2, 2016 at 2:41am
gcd(a,b,c) = gcd(a,gcd(b,c))
Use recursion :)
Topic archived. No new replies allowed.