Write a program to find the greatest common factor if the two numbers are provided in the main function.
Use pass by value mechanism to compute the greatest common factor and return the result back to the main
function then display the result in the main function.
#include <iostream>
using namespace std;
int greatest_common_factor(int x, int y) {
if (y == 0) {
return x;
}
return greatest_common_factor(y, x%y);
}
int main() {
int x, y;
cout << "Enter two integer numbers: ";
cin >> x >> y;
int gcf = greatest_common_factor(x, y);
cout << "The greatest common factor is " << gcf << endl;
return 0;
}
Comments
Leave a comment