Help with greatest common factor program

Dec 23, 2012 at 12:28am
I want a function to do something, but then I also want it to print what it was doing. I want it to find the greatest common factors of two numbers using

if (a % b ==0)

return b;

else

return gcf(b,a % b);

I want it to return the whole process if that makes sense.
Dec 23, 2012 at 12:44am
That looks the correct body for an implementation of the Euclidean algorithm. What's the problem?
Dec 23, 2012 at 1:51am
Sorry for not being clear I want it to output the whole thing so you can see the entire mathematical process if that makes sense.

gcf 560 % 340 = 220
gcf 340 % 220 = 120
gcf 220 % 120 = 100
gcf 120 % 100 = 20
gcf 100 % 20 = 0

I can't for the life of me figure out how to make it print that out.
Dec 23, 2012 at 1:59am
1
2
3
4
int gcd(int a,int b){
    std::cout <<a<<" % "<<b<<" = "<<a%b<<std::endl;
    return (a%b==0) ? b : gcd(b,a%b);
}
Topic archived. No new replies allowed.