Help me

Discussion in 'C++' started by bashamsc, Nov 28, 2007.

  1. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai
    I have written a program for factorial in recursive and i am not able get how the program work.
    The program is as follows
    Code:
    [COLOR=Green]#include<iostream.h>
    main()
    {
    int fact(int m);
    unsigned long int f;
    cout<<"enter the no. "<<endl;
    int n;
    cin>>n;
    f = fact(n);
    cout<<"factorial of the no. is  "<<f<<endl;
    }
    int fact(int m)
    {
    unsigned long int f=1;
    if(m==0|m==1)
    return(m);
    else
    {
    cout<<"before m = "<<m<<endl;
    f=m*fact(m-1);
    cout<<"after m = "<<m<<endl<<"fact(m-1) =  "<<fact(m-1)<<endl;
    cout<<"f = "<<f<<endl;
    return(f);
    }
    }[/COLOR]
    
    I am getting out put like this. Can any one explain me why i am getting like this
    enter the no.
    3
    before m = 3
    before m = 2
    after m = 2
    fact(m-1) = 1
    f = 2
    before m = 2
    after m = 2
    fact(m-1) = 1
    f = 2
    after m = 3
    fact(m-1) = 2
    f = 6
    factorial of the no. is 6
     
    Last edited by a moderator: Nov 28, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    if(m==0|m==1) should be if(m==0||m==1) see the sign of OR its ||
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice