Q38.. Write a C++ program to find out GCD of given two numbers using function.
Answer :
Program
Output
Answer :
Program
#include<iostream.h>
#include<conio.h>
int gcd(int a,int b);
int main()
{
int p, q;
clrscr();
cout<<“Enter the values of p and q:”;
cin>>p >>q;
cout<<“GCD of” <<“ "<< p<<“
"<< “and” <<“ ” << q <<“ "<<
“is”<<“ "<< gcd(p,q);
getch();
return 0;
}
int gcd(int a,int b)
{
if(b==0)
return a;
else if(b>a)
return gcd(b,a);
else
return gcd(b,a%b);
}
Output