plz find the error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikasdev
    New Member
    • Jan 2008
    • 4

    plz find the error

    [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
    Last edited by Ganon11; Feb 17 '08, 03:08 PM. Reason: Please use the [CODE] tags provided.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I would actually recommend the explanation of how recursion works before debugging - both Google and Wikipedia have good articles on this. You have the internet at your fingertips, and those resources were created to help find and explain things like that.

    As for debugging, why would someone else do your homework for you? Especially when you admittedly have a less than clear understanding of how your program works. This is why you should develop a precise algorithm before coding your project - if you just jump into the coding, you will get stuck and confused.

    Comment

    Working...