Computer Science, asked by vinijoshi59211, 9 months ago

Write a program to compute GCD of 2 numbers using recursion. INPUT & OUTPUT FORMAT: Input consists of 2 integers. Refer sample input and output for formatting specifications. SAMPLE INPUT & OUTPUT: 36 27 G.C.D of 36 and 27 = 9

Answers

Answered by jjzach21
10

Answer:

Explanation:

#include <iostream>

using namespace std;

int func(int a,int b)

{

 int small;

 int gcd;

 if(a<b)

 {

   small=a;

 }

 else  

 {

   small=b;

 }

 

 while(small>=1)

 {

   if((a%small==0)&&(b%small==0))

   {

     gcd=small;

     

     break;

   }

 small--;

 }

 

}

int main()  

{

 int a;

 int b;

 int g;

 cin>>a>>b;

 g=func(a,b);

 cout<<"G.C.D of "<<a<<" and "<<b<<" = "<<g;

}

Answered by MSFLAB3
1

Answer:

#include<iostream>

int gcd(int x,int y,int a)

{

 if(y==1)

   return 1;

 if(x%y==0 && a%y==0)

   return y;

 return gcd(x,y-1,a);

}

int main()

{

 int n1,n2,s,l;

 std::cin>>n1>>n2;

 if(n1<n2)

 {

   s=n1;

   l=n2;

 }

 else

 {

   s=n2;

   l=n1;

 }

 std::cout<<"G.C.D of "<<n1<<" and "<<n2<<" = "<<gcd(l,s,s);

}

Similar questions