Tuan Nguyen
Tuan Nguyen

Reputation: 197

Program calculating power of 2

I'm writing a simple program to calculate power of 2. A user would enter the number of times they want to calculate the power of 2, let say the user enter 4, my program would returns 2,4,8,16.Here is the code

import java.util.Scanner;

public class PowersOf2

{

public static void main(String[] args)

{

int numPowersOf2;        
//How many powers of 2 to compute

int nextPowerOf2 = 1;    
//Current power of  2

int exponent = 0;            

//Exponent for current power of 2 -- this

//also serves as a counter for the loop

Scanner scan = new Scanner(System.in);

        System.out.println("How many powers of 2 would you like printed?");
        numPowersOf2 = scan.nextInt();

        //print a message saying how many powers of 2 will be printed
        //initialize exponent -- the first thing printed is 2 to the what?

    System.out.println("Here are the first " + numPowersOf2 + " power of 2");


      while (exponent<numPowersOf2)
        {
          //print out current power of 2

            nextPowerOf2=nextPowerOf2*2;

            exponent++;

            System.out.println(nextPowerOf2);
            //find next power of 2 -- how do you get this from the last one?

            //increment exponent
             }
        }
}

If I want it to start at 0 first say like 2^0=1, so if user enter 4, it would give back 1,2,4,8 instead of 2,4,8,16. How do I modify this to get that?

Upvotes: 1

Views: 33382

Answers (8)

user207421
user207421

Reputation: 310840

2n == (1 << n); 0 <= n < 32

Upvotes: 5

Carlos
Carlos

Reputation: 1

Instead of using :

while (exponent < numPowersOf2)

Use This:

while (exponent <= numPowersOf2)

// <= will start with 0, which is 2^0=1

Upvotes: 0

int numpowerof2 = 4, nextpower0f2 = 2, exponent = 0;
while(exponent < numpowerof2) {
  System.out.println(nextpower0f2);
  nextpower0f2 = nextpower0f2 * 2;
  exponent++;
}

Upvotes: 0

Jeremy Trifilo
Jeremy Trifilo

Reputation: 470

Try these? They are quite fast and fit most needs previous, current, and next.

EDIT: noticed you wanted it to print out all, well what you could do is where the while loop's are add a print out.

public static int getPowerOfTwo(int size)
{
    int n = -1;
    while (size >> ++n > 0);
    return (1 << n - 1 == size) ? size : 1 << n;
}

public static int getNextPowerOfTwo(int size)
{
    int n = -1;
    while (size >> ++n > 0);
    return 1 << n;
}

public static int getPreviousPowerOfTwo(int size)
{
    int n = -1;
    while (size >> ++n > 0);
    return 1 << n - 1;
}

Just in case you can't figure it out the below should work.

public static void displayNextPowersOfTwo(int size)
{
    int n = -1;
    while (size >> ++n > 0) {
         System.out.print((1 << n) + ",");
    }
    System.out.print('\n');
}

Upvotes: 0

Goz
Goz

Reputation: 62323

 nextPowerOf2 = 1;
 exponent     = 0;
 do
 {
     System.out.println(nextPowerOf2);
     nextPowerOf2=nextPowerOf2*2;
     exponent++;
 }
 while( exponent < numPowersOf2 );

That should do it.

Upvotes: 0

Thomas
Thomas

Reputation: 181705

First print, then update. Just a reordering of operations:

while (exponent<numPowersOf2)
{
    //print out current power of 2
    System.out.println(nextPowerOf2);

    //find next power of 2
    nextPowerOf2=nextPowerOf2*2;

    //increment exponent
    exponent++;
}

Now with correct indentation and comments in sensible places, too!

Incidentally, it makes more sense to write this with a for loop:

for (int exponent = 0; exponent < numPowersOf2; ++exponent) {
    //print out current power of 2
    System.out.println(nextPowerOf2);

    //find next power of 2
    nextPowerOf2=nextPowerOf2*2;
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533432

You should ONLY print the value before multiplying by 2.

 //print out current power of 2
 System.out.println(nextPowerOf2);
 nextPowerOf2=nextPowerOf2*2;
 exponent++;

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359776

Just print nextPowerOf2 before you change it.

while (exponent<numPowersOf2)
{
    System.out.println(nextPowerOf2);

    nextPowerOf2=nextPowerOf2*2;
    exponent++;
}

Upvotes: 3

Related Questions