GCD by Using Recursion
GCD by Using Recursion
#include<iostream>
return n2;
return n1;
if(n1==n2) //if both n1 and n2 becomes equal the any one is gcd
return n1;
if(n1>n2)
return GCD(n1-n2,n2); //if n1>n2 the call the function again with passing the argument as n1 -n2
and n2 as its is
return GCD(n1,n2-n1); //if n2>n1 then call the function again with passing the argument as n2-n1 and
n1 as it is
int main()
{
cin>>a;
cin>>b;
cout<<GCD(a,b)<<endl; //the recursive function is called first from here and the the ans returns here
return 0;
OUTPUT:-