Hey there,
this code was supposed to work out th GCD of 2 numbers
added cout line in function and code seems to be running fine
but it returns 4249024 instead of 29
this is probably a basic error on my part but can anyone tell me why?
thanks for any help
as is quite clear am a beginner at this.

#include <iostream>
#include <cmath>

using namespace std;

int g_c_d(int a,int b);

int main()
{
    cout<<g_c_d(2871,4060);/*test example*/
    
    
    cout<<endl;   
    system("pause");/*been advised not to use this but at my level well hey!*/
    return 0;
}

int g_c_d(int a,int b)
{
    int c;
    c=abs(b-a);
    cout<<a<<" "<<b<<" "<<c<<endl;/*put this in to check it was going ok*/
    if(c==0){return a;}
    if(b>a){b=a;}
    a=c;
    g_c_d(a,b);
}

Line 26 calls g_c_d recursively and throws the result away.

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.