0

Hi,

i have written a program to find the power of two numbers using iteration.

The problem here is that to return the correct out put for even powers but not for odd powers.

b = base , e = expo ,p= power( b, e);;

for even powers the statements in the if ( e % 2 == 0) are enough.

for odd powers we need to perform one extra multiplication with base.

but i am unable to control the if and else properly.

if i control else it will become a probelm in if or vice versa.

please suggest me on this.

i want to perform the power with order ( log n ) and order (1).

```
int power( int b , int e)
{
int p = 0;
if ( e < 0 )
return NEG_POWER;
if ( e == 0)
return 1;
if ( b == 0 )
return 0;
while ( e )
{
if ( e % 2 == 0) {
p = b*b;
e/=2;
}
else if ( e % 2 == 1 ) {
p = b*p;
}
}
}
```

i am trying foll way:

but its a problem in if for odd powers.

```
int e2 = e ; // at the start
else if ( e % 2 == 1 ) {
if ( e2 % 2 == 0)
return p;
else {
p = b*p;
e /= 2;
}
}
```