Recursion

This is a discussion on Recursion within the C Programming forums, part of the General Programming Boards category; I have a exercise which asks that a function be writen that returns the value of base and exponent using ...

  1. #1
    RWS
    RWS is offline
    Registered User
    Join Date
    May 2002
    Posts
    1

    Recursion

    I have a exercise which asks that a function be writen that returns the value of base and exponent using recursive function.
    for example; integerpower (3,4) = 3*3*3*3. Assume that the exponent is a positive nonzero integer, and base is an integer. The function should use for to control the calculation. Can you explain what the original call code will make this work?

  2. #2
    Unregistered
    Guest
    Please restate your question. I find that a lot of people post questions about programming, but should really be learning to write english instead.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,855
    I think you want something more like this:
    Code:
    /* Recursive factorial function */
    
    #include <stdio.h>
    
    long integerpower (int base, int exponent);
    
    int main()
    {
      int  x, y = 2;
      for ( x = 1; x <= 10; x++ )
        printf ("%d^%d\tis %d\n", y, x, integerpower ( y, x ) );
      return 0;
    }
    
    long integerpower (int base, int exponent)
    {
      if (exponent == 1)
        return base;
      else
        return(base * integerpower(base, exponent -1));
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09:56 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21