0% found this document useful (0 votes)
6 views2 pages

GCD by Using Recursion

Uploaded by

asrajput9015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

GCD by Using Recursion

Uploaded by

asrajput9015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

//****** Program to make GCD by using recursion ********

#include<iostream>

using namespace std;

int GCD(int n1, int n2)

if(n1==0) //if n1==0 then gcd is n2

return n2;

if(n2==0) //if n2==0 then n1 is gcd

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()
{

int a, b; //taking the numbers for gcd from user

cout<<"Enter First Number: ";

cin>>a;

cout<<"Enter Second Number: ";

cin>>b;

cout<<"GCD of Numbers is: ";

cout<<GCD(a,b)<<endl; //the recursive function is called first from here and the the ans returns here

return 0;

OUTPUT:-

You might also like