hi there, can someone help me how to write a recursive function that takes the number to the power. Thus, if the number is 2 and the power is 4, the function will return 16.

I'm looking forward for anyone to help me with this my problem.

I try to solve it to myself but i can't find the right code for this.

Thanks...

4
Contributors
11
Replies
12
Views
9 Years
Discussion Span
Last Post by iamthwee

Post what you have tried.

Post what you have tried.

``````#include <iostream.h>
#include <stdlib.h>

int byThePower(int number, int power);
int main()
{
cout <<"Enter a number: ";
cin >>number;
cout <<"\nEnter a Power: ";
cin >>power;

cout <<"\n\n";
system("PAUSE");
return 0;
}

int byThePower(int number, int power)
{
int ans,num,power1 ;
num =1;
if (power != 0)
{
number=number*number;
power--;
return(byThePower(number,power));
}
else
{
return number;
}

}``````

Thanks

``````int ans,num,power1 ;
num =1;``````

Why you declare something that you didn't use?

``````number=number*number;
power--;
return(byThePower(number,power));``````

Let say that the number is 2 and the power is 4 then with this code it would process like this.

• 2 x 2 = 4
• 4 x 4 = 16
• 16 x 16 = 256
• 256 x 256 = 65536

yeah that was the output of my program.. but i want my program to output like this:

2^4 = 16 or
3^3 = 27.
I don't know what is the right code to achieve this... can you help me to give the right code and explain why....

I hope this is not too much for you but im very thankful if you could help me..

>>3^3 = 27.
that's the wrong answer, so you program will never get it (unless the program is wrong) :)

you over-complicated your program -- the solution is much simpler

``````int byThePower(int number, int power)
{
if(power >1)
number = number * byThePower(number,power-1);
return number;

}``````

I would write it like this: (this code works only when the power is a positive number)

``````int byThePower(int number, int power)
{
if (power >= 1)
return number * (byThePower(number , power - 1));
else
return 1;
}``````

I would write it like this: (this code works only when the power is a positive number)

You are right -- the solution I posted doesn't work for power of 0.

I would write it like this: (this code works only when the power is a positive number)

``````int byThePower(int number, int power)
{
if (power >= 1)
return number * (byThePower(number , power - 1));
else
return 1;
}``````

it's better if it is like this:

int byThePower(int number, int power)
{
if (power != 0)
return number * (byThePower(number , power - 1));
else
return 1;
}

can you explain how this code works: return number * (byThePower(number , power - 1));

``return number * (byThePower(number , power - 1));``

I don't know how to explain it, so I would rather show how it process: Let say power = 4

• Number to return = number * byThePower(number, 3)
• byThePower(number, 3) = number * byThePower(number, 2)
• byThePower(number, 2) = number * byThePower(number, 1)
• byThePower(number, 1) = number * byThePower(number, 0)

Because of `byThePower(number, 0)` will probally return 1 so that.

• byThePower(number, 1) = number * 1 = number
• byThePower(number, 2) = number * byThePower(number, 1) = number * number
• byThePower(number, 3) = number * byThePower(number, 2) = number * number * number
• Number to return = number * byThePower(number, 3) = number * number * number * number

Moreover, I prefer `if (power >= 1)` than `if (power != 0)` since it is safer. If you use `if (power != 0)` and the power is a negative number, then it leads to infinitive loop.

``return number * (byThePower(number , power - 1));``

I don't know how to explain it, so I would rather show how it process: Let say power = 4

• Number to return = number * byThePower(number, 3)
• byThePower(number, 3) = number * byThePower(number, 2)
• byThePower(number, 2) = number * byThePower(number, 1)
• byThePower(number, 1) = number * byThePower(number, 0)

Because of `byThePower(number, 0)` will probally return 1 so that.

• byThePower(number, 1) = number * 1 = number
• byThePower(number, 2) = number * byThePower(number, 1) = number * number
• byThePower(number, 3) = number * byThePower(number, 2) = number * number * number
• Number to return = number * byThePower(number, 3) = number * number * number * number

Moreover, I prefer `if (power >= 1)` than `if (power != 0)` since it is safer. If you use `if (power != 0)` and the power is a negative number, then it leads to infinitive loop.

Now i understand, thanks for helping me and giving information...
thanks for everything....

>>3^3 = 27.
that's the wrong answer, so you program will never get it (unless the program is wrong)

Wuh?