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;
    }

}

Dani AI

Generated

Short answer: call the GCD function once, store its return, and print that one value. As pointed out, calling GCD(n1,n2) twice with the same inputs just gives the same result twice. was right to ask for example input/expected output — that helps diagnose whether the logic or the usage is the problem.

Two practical improvements to make the program cleaner and more robust:

  • Compute once and use a clear variable name: result or g instead of reusing a/b which can be confusing.
  • Handle bad input and edge cases: confirm numeric input, consider negative values (GCD is normally non-negative), and decide how to treat the (0,0) case (many libraries return 0, but you may want to treat it as undefined).

If using a modern compiler, prefer the standard library implementation (C++17+):

#include <iostream>
#include <numeric> // std::gcd

int main() {
    long long n1, n2;
    if (!(std::cin >> n1 >> n2)) return 1;
    std::cout << std::gcd(n1, n2) << '\n';
}

Compile with C++17 or later (for example: -std=c++17). See std::gcd on cppreference.

If you need a custom implementation (pre-C++17), use a clear Euclidean loop that takes absolute values and returns once:

long long gcd(long long a, long long b) {
    if (a < 0) a = -a;
    if (b < 0) b = -b;
    while (b) {
        long long t = a % b;
        a = b;
        b = t;
    }
    return a;
}

Final checklist: remove unused headers (like math.h or iomanip if unused), avoid using namespace std; in larger projects, and keep prompts/outputs simple so the result is obvious.

Recommended Answers

All 4 Replies

What do you mean "return only one of the values"? A function can only return one value, and it looks like yours is... maybe you can give an example input, expected output, and current output?

it's a greatest common divisor. you input 2 numbers and it spits out the GCD. look at the code. I want it to return 'a' or 'b' depending on which on is the GCD. it right now returns 'a' and 'b'

What do you mean "return only one of the values"? A function can only return one value, and it looks like yours is... maybe you can give an example input, expected output, and current output?

commented: ... -4
a = GCD(n1,n2);
	b = GCD(n1,n2);

The values of a and b will be the same because the values of n1 and n2 do not change between the function calls. There is no point in calling GCD() twice with the same parameters.

yeah I just figured that out as I was checkin to see if there was a reply to this post. I can't believe I missed something so simple. thanks. but the rest is good right. i mean it has to be since it works. but I'm trying very hard to be better at formatting and making sure things are nice and neat. thanks

a = GCD(n1,n2);
	b = GCD(n1,n2);

The values of a and b will be the same because the values of n1 and n2 do not change between the function calls. There is no point in calling GCD() twice with the same parameters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.