Answer to Question #128974 in C++ for ikkfeat

Question #128974
Need a function that finds the GCD of two integers.
1
Expert's answer
2020-08-09T08:44:57-0400
#include <iostream>
using namespace std;

int gcd(int x, int y)
{
    if(y==0)  // base case
        return x;
    return gcd(y, x%y);  // recursive call
}

int main()
{
    int m, n;
    cout << "Enter two numbers: ";
    cin >> m >> n;
    cout << "GCD(" << m << ", " << n << ") = " << gcd(m, n) << endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS