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;
                    }
}

Why does it require such complicated code just to raise b^e using iteration? It's just a simple loop. Work the math out on paper then code it. You don't need % or / operators. Just * operator and a simple for loop without those if statements.

>>i want to perform the power with order ( log n ) and order (1).
My solution may or may not meet that requirement.

How about

else if ( e % 2 == 1 ) {
                         p = b*p;
                         e -= 1;
                 }

How about

else if ( e % 2 == 1 ) {
                         p = b*p;
                         e -= 1;
                 }

p is getting overwritten here how will we add that here.

if ( e % 2 == 0) {
       p = b*b;
      e/=2; 
 }

p is getting overwritten here how will we add that here.

if ( e % 2 == 0) {
       p = b*b;
      e/=2; 
 }

Oh. I didn't realize you are doing that.

Back to the original:

while ( e )
         {       
                 if ( e % 2 == 0) {
                         p = b*b;
                         e/=2;
                 }
                 else if ( e % 2 == 1 ) {
                         p = b*p;
                 }
         }

Let's say, e is a power of 2, so the code never enters the else clause. Can you tell what would be the result?

Oh. I didn't realize you are doing that.

Back to the original:

Let's say, e is a power of 2, so the code never enters the else clause. Can you tell what would be the result?

what ever is the value of 'e' it will definently become 1 at the last iteration.

now the problem is

if e is even initially the return value should be the value in if part, if not the return value should be base multiplied by the value in the if part

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.