Recursive Function Trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • juanc
    New Member
    • Aug 2007
    • 2

    Recursive Function Trouble

    i have to do a program that read a number and say if it is a power of 3, using recursivity function.
    i already did but something is not right, please help
    thanks


    [CODE=cpp]# include <iostream.h>
    # include <conio.h>

    bool powerof3 (int n)
    {
    if((n%3)==0)
    {
    n=n/3;
    powerof3(n);
    }
    else
    {
    if (n==1)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }
    main()
    {
    int n;
    bool a;
    cout<<"escriba un numero"<<endl;
    cin>>n;
    a=powerof3(n);
    if (a==true)
    {
    cout<<"el numero "<<n<<" es una potencia de 3"<<endl;
    }
    if (a==false)
    {
    cout<<"el numero "<<n<<" no es una potencia de 3"<<endl;

    }
    getch();
    }[/CODE]
    Last edited by Ganon11; Aug 29 '07, 01:10 AM. Reason: Please use [CODE] tags to wrap around your code.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    A few things:

    1) Please use the [CODE] tags available in the text editor box when including code in your post. Just highlight the code and press the # symbol above your text. These tags make the code much easier to read.

    2) Please use a clearer title to your post. When many experts see a thread with an unclear title, they may skip over it, not knowing if they'll be able to help or not. Using a clear thread title will give experts a better idea of whether or not they can help you. In general, stray away from titles such as "Help me," and try to use titles that indicate the nature of your trouble.

    3) Please post in the proper section. I have moved this thread from the Miscellaneous Questions forum to the C++/C forum.

    Comment

    • gsi
      New Member
      • Jul 2007
      • 51

      #3
      Hi,
      It should be return(powerof3 (n)); in line 9. A function returning bool returns false by default, if return value is not specified (May be implementation defined).
      Thanks,
      gsi.

      Comment

      • juanc
        New Member
        • Aug 2007
        • 2

        #4
        thanks its working now

        Comment

        Working...