C++ Program to Find GCD or HCF of Two Numbers Using Recursion

C++ program to calculate GCD or HCF of two numbers using recursion.


C++ program to calculate GCD using recursion

#include <iostream>

using namespace std;
  
int getGcd(int a, int b);

int main(){
    int num1, num2, gcd;
    
 cout << "Enter two numbers\n";
    cin >> num1 >> num2;
    
    gcd = getGcd(num1, num2);

    cout << "GCD of " << num1 << " and " << num2 << " is " << gcd;

    return 0;
}

int getGcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  else {
    return getGcd(b, a % b);
  }
}
Output
Enter two numbers
8 60
GCD of 8 and 60 is 4

Recommended Posts
C++ Program to Find GCD of Two Numbers
C++ Program to Find Factorial of a Number
C++ Program to Reverse Digits of a Number
C++ Program to Find Power of a Number
C++ Program to Check Prime Number
C++ Program to Find Sum of Natural Numbers Using Recursion
C++ program to Find Factorial of a Number Using Recursion
C++ Program to Find Power of Number using Recursion
C++ Program to Find Area and Perimeter of Parallelogram
All C++ Programs