# Thread: How to write a Power() method?

1. ## How to write a Power() method?

I'm stuck on this program in this book I'm reading. It basically wants me to write my own Power() method and return the value. If someone knows how to do this. That would be much appreciated. Here's my code

Code:
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapter9Problem7
{
class Program
{

static void Main()
{
int x = 0;
int y = 0;
int result;

Console.Write("\nEnter BASE: ");

Console.Write("\nEnter EXPONENT: ");

result = Power(x,y);

Console.Write("\nBASE:{0}  EXPONENT:{1} = {2}", x, y, result);

}

static int Power(int x, int y)
{

for (int i = 1; i <= y; i++)
return
}

}
}```

I can't figure out the return part of it so I just left it That's where I'm stuck at 2. You need to compute the result in the loop, and return it after the loop has finished.

Given that the point of raising to an (integral power), all the loop will do is multiply by something by x the required number of times. 3. Well I think I figured it out it was real simple just

return x * y; 4. Gosh!! Me feels stupid!! heehee 5. Well I feel even more stupid! I'm wrong it's not

return x * y;

I don't have a clue how to times x y number of times 6. I got to thinking maybe I should use doubles 7. Code:
```       static int Power(int x, int y)
{
for (int i = 1; i <= y; i++)
return x * i;
}

}```
when I try this code it gives me not all paths return value error 8. That is because y could be less than 1.

Remember with powers you are raising the value to itself.

2^3 = 2*2*2 = 8

So you need to change your logic since what you are doing currently you are multiplying x times i once and exiting the function. 9. Originally Posted by artistunknown I don't have a clue how to times x y number of times
0) Read all of the steps below and understand them, or print them out.

2) Write down on paper the exact steps you would need to go through to do the calculation your self

3) Check that your approach correctly hands special case of x = 0 and x = 1. If it doesn't, your approach is wrong, so return to step 2 and start again. Bear in mind that 0 to the power of 0 does not have a defined result.

4) Pick a couple of arbitrary values of x that are neither 0 nor 1, one positive and one negative. Pick a couple of positive values of y. Test your approach with the the combinations of those values of y. If you don't get the right result for any of those combinations, your approach is wrong so return to step 2 and start again.

5) Based on your approach, write down some pseudo-code that would produce the same result.

7) Edit a source file, and work out a C equivalent of your pseudo-code.

9) Test your program with the values of x = 0 and x=1, and for the combinations of values you used in step 4. If your program produces the wrong result, return to step 7.

10) If you've got this far, you have a program producing the right results. Rejoice. 10. Code:
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapter9Problem7
{
class Program
{

static void Main()
{
int x = 0;
int y = 0;

Console.Write("\nEnter BASE: ");

Console.Write("\nEnter EXPONENT: ");

Console.Write("\nBASE:{0}  EXPONENT:{1} = {2}", x, y,Power(x,y));

}

static int Power(int x, int y)
{
int result = 1;
while (y>0)
{
y--;
result *= x;
}
return result;
}

}
}```
Well sorry Grumpy I didn't take your advice I wouldn't have been able to figure out this one no matter how long I worked on it I found the answer on the internet and changed it a little to make it work this is what I came up with Thanks for the help 11. Originally Posted by artistunknown Well sorry Grumpy I didn't take your advice I wouldn't have been able to figure out this one no matter how long I worked on it I found the answer on the internet and changed it a little to make it work this is what I came up with Thanks for the help
So you've got the answer, but learned nothing of value. If you can't reason out an answer to a trivial learning exercise like this, you won't have a hope with real-world problems. 12. Yeah, I know but I just don't want to give up just yet. 13. Well at least I understand the code now I was a little confused by it at first
I did attempt to work out the problem on my dry erase board but I just couldn't think outside my own little box 14. Well with the logic I was writing I guess I could have written the Power function like this

Code:
```       static int Power(int x, int y)
{
int result = 1;
for (int i = y; i >= 1; i--)
{

result = result * x;

}
return result;

}``` 15. Code:
```        static int Power(int x, int y)
{
int result = 1;
for (int i = 1; i <= 9; i++)
{

result = result * x;

}
return result;

}```
This is the closest to what I was doing I just couldn't figure out the body Hopefully I will get better if not off to culinary school hee hee Popular pages Recent additions 