So, I am new to C++ and only just started. I have bought a book called C++ Without Fear by Brian Overland, and I am having a problem with one of the exercises to do with the Greatest Common Factor.
The source code is:
Code:
#include <iostream>
using namespace std;
int gcf(int a, int b);
int main() {
int a = 0, b = 0;
while(1) {
cout << "Enter a number (0 to quit): ";
cin >> a;
if (a==0)
break;
cout << "Enter 2nd number: ";
cin >> b;
cout << "GCF = " << gcf(a, b) << endl;
}
return 0;
}
int gcf(int a, int b) {
if (a % b == 0)
return b;
else
return gcf(b, a % b);
cout << gcf(b, a % b);
}
That much I have managed to understand, but now after he explains it, he says to do an exercise where I revise the program so it shows all the steps involved in the algorithm.
For example-
GCF(300, 500) =>
GCF(500, 300) =>
GCF(300, 200) =>
GCF(200, 100) =>
GCF = 100
Right now, all that the code does is immediately give the answer.
Please help me out.
Thanks!