top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to write power(x,y) i.e. x^y recursively?

+2 votes
388 views

How to implement the function power(int x, int y) recursively.

posted Nov 17, 2013 by As M Ob

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
if I understood your question correctly, you are looking a program for X^y rather than just x*y.

1 Answer

+2 votes
int power(int x, int y)
{
   if (y>0)
      return (x * power(x, y-1));
   else
      return 1;
}
answer Nov 17, 2013 by Vimal Kumar Mishra
int power(int x, int y)
{
   if (y==1)
      return X;
   else
      return (x * power(x, y-1));
}

save memory for one stack :) :P
Similar Questions
+5 votes

How to compute modulo power of 10,000 digit number to 10,000 digit number using C language
Eg:-(1234567891111123433434^211222324334343422233)%**********

And how to store and retrieve such a large values

+2 votes

1,1,2,2,2,6,6,6,7,7,7,7,7,7,7,8,8,9,9,9

Example:
Input = 1 Output=0 (First index of 1).
Input = 2 Output=2 (First index of 2).
Input = 6 Output= 5 (First index of 6).
Input = 7 Output= 8 (First index of 7).
Input = 8 Output=15 (First index of 8).
Input = 9 Output=17 (First index of 9).

+4 votes
                                      *
                                     * *
                                    * * *
                                   * * * *
                                  * * * * * 
                                 * * * *     

I above Ouput I Missed two stars.My question is how to find out the remaining stars.Please tel me in the C programming way.

...