Thread: power recursion

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    power recursion

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int power ( int, int );
    
    int main()
    {
        int base, exp;
    
        printf("?");
        scanf( "%d%d", &base, &exp );
    
        printf( "awn == %d\n", power( base, exp ) );
    
          system("PAUSE");
          return 0;
    }
    
    int power ( int base, int exp )
    {
    
     if ( exp == 1 )
        return exp;
     else
         return ( power( base, exp -1 ) * base );
    }
    It uses a recursion function to find the power of int ^ itn ..Well the output is not right where is the problem i never did read the recursion part of the book and know i have to suffer ...

    if i enter 2 ^ 4 it gives me 8 instead of 16 ???
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Change this
    Code:
     if ( exp == 1 )
        return exp;
    to this
    Code:
     if ( exp == 1 )
        return base;
    }
    because x^1 = x and x^0 = 1
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    thank man
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 11:56 AM
  3. Recursion question
    By InvariantLoop in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2004, 05:31 PM
  4. power of 2? without loops or recursion
    By rahuls in forum C Programming
    Replies: 8
    Last Post: 03-20-2003, 05:10 PM