I'm trying to get a GCD of 2 numbers. which I can do and it works fine. however I want to return one of the values based on the function I wrote. however it returns both. I know why because I wrote it that way just to get something down. but how can I just return one value? I'm sure it's something easy, but I'm not seeing it.
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
//---------------------------------------------------------------------------
int GCD (int, int); //prototype
int main()
{
int n1, n2, a, b;
cout << "This program calculates the GCD of 2 integers. " << endl;
cout << "Please enter 2 numbers. " <<endl;
cout << "First number is : " <<endl;
cin >> n1;
cout << "Second number is : " << endl;
cin >> n2;
cout << endl;
a = GCD(n1,n2);
b = GCD(n1,n2);
cout << "The Greatest Common Divisor of the numbers you entered is: " <<endl;
cout << a <<endl;
cout << b << endl;
return 0;
}
int GCD(int a, int b)
{
while( 1 )
{
a = a % b;
if( a == 0 )
return b;
b = b % a;
if( b == 0 )
return a;
}
}