C++ Program to Find G.C.D Using Recursion

C++ Program to Find G.C.D Using Recursion

In this example, we will learn a program to to find the sum of natural numbers by using a recursive function.

To understand this example, you should have the knowledge of the following C++ programming topics:

G.C.D Using Recursion

This program takes two positive integers from user and calculates "GCD" using recursion.

Visit this page to learn, how you can calculate GCD using loops.

Example: Program to Calculate H.C.F 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);
       return n1;

Output 1

Enter two positive integers: 366 60
H.C.F of 366 and 60 is: 6

Next Example

We hope that this Example helped you develop better understanding of the concept of "Find G.C.D Using Recursion" in C++.

Keep Learning : )

In the next Example, we will learn about C++ Binary Number to Decimal and vice-versa.

- Related Topics