Calculate Gcd Of Two Numbers Using Recursion in CPP

  1. Home
  2. Tutorials
  3. CPP
  4. CPP Programs
  5. Recursion
  6. Program

Source Code

#include <iostream>

using namespace std;

int GCD( int );

int main(int argc, char** argv) {
 

int a,b;  // variable declaration for input
    
    // Taking input in variables
    cout<<"Enter First Number : ";
    cin>>a;

    cout<<"Enter Second Number : ";
    cin>>b;
    cout<<"GCD : "<<GCD(a,b);

return 0;
}

int GCD( int x , int y) {
    
if ( y != 0 )
    return GCD( y , x % y );

else
       return x;
}

Output

find greatest common divisor GCD using recursion in c++

Working

Greatest common divisor is the common largest number that can divide the provide numbers. In this program example recursion is used for GCD calculation. This recursive call will get remainder of given two numbers until one of these equal to zero.

Comments
Login to TRACK of Comments.