[CODE=cpp]#include<iostre am>
using namespace std;
int hcf(int,int);
int main(){
int x,y,div,dividen d;
cout<<"enter two numbers:\n";
cin>>x>>y;
if(x>y){
div=y;
dividend=x;
}
else{
div=x;
dividend=y;
}
int q=hcf(div,divid end);
cout<<"\ngcd is:"<<q;
}
int hcf(int div,int dividend){
int r;
r=dividend%div;
cout<<r<<" "<<div<<"\n ";
if(r!=0)
hcf(r,div);
else
return(div);
}[/CODE]
here it is not giving correct output for 2 numbers. whose gcd is 1.Can u plz debug it and tell me how exactly recursion works
using namespace std;
int hcf(int,int);
int main(){
int x,y,div,dividen d;
cout<<"enter two numbers:\n";
cin>>x>>y;
if(x>y){
div=y;
dividend=x;
}
else{
div=x;
dividend=y;
}
int q=hcf(div,divid end);
cout<<"\ngcd is:"<<q;
}
int hcf(int div,int dividend){
int r;
r=dividend%div;
cout<<r<<" "<<div<<"\n ";
if(r!=0)
hcf(r,div);
else
return(div);
}[/CODE]
here it is not giving correct output for 2 numbers. whose gcd is 1.Can u plz debug it and tell me how exactly recursion works
Comment