0

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);
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by arkoenig
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.