C++ Program to Find GCD Using Recursion

195
views
Program to Find GCD Using Recursion
(Last Updated On: September 1, 2018)

In this Programme, you’ll learn Program to Find GCD Using Recursion in C++.

To nicely understand this example to Find GCD Using Recursion, you should have the knowledge of following C++ programming topics:

  • Recursion
  • C++ if, if…else and Nested if…else
  • Functions
  • Types of User-defined Functions

Here in this program, we have to provide 2 positive numbers and we’ll get GCD of those two numbers.

Program to Find GCD Using Recursion

#include <iostream>
using namespace std;

int hcf(int n1, int n2);

int main()
{
   int n1, n2;

   cout << "Enter two positive integers: ";
   cin >> n1 >> n2;

   cout << "H.C.F of " << n1 << " & " <<  n2 << " is: " << hcf(n1, n2);

   return 0;
}

int hcf(int n1, int n2)
{
    if (n2 != 0)
       return hcf(n2, n1 % n2);
    else 
       return n1;
}

Output:

Enter two positive integers: 200 500                                                                                           
H.C.F of 200 & 500 is: 100

Check out these related Programs:

Ask your questions and clarify your/others doubts on how to Find G.C.D Using Recursion by commenting. Documentation