# Thread: Powers without Math.pow ???

1. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Powers without Math.pow ???

I am having trouble with this, finding powers without math.pow does not really make sense. any help with this would be great.

instructions are:

File PowersOf2.java contains a skeleton of a program to read in an integer from the user and print out that many powers of 2, starting with 2^0.

1. Using the comments as a guide, complete the program so that it prints out the number of powers of 2 that the user requests. Do not use Math.pow to compute the powers of 2! Instead, compute each power from the previous one (how do you get 2n from 2n–1?). For example, if the user enters 4, your program should print this:

Here are the first 4 powers of 2:
1
2
4
8

2. Modify the program so that instead of just printing the powers, you print which power each is, e.g.:

Here are the first 4 powers of 2:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8

Here is my code so far:
Java Code:
```//   Print out as many powers of 2 as the user requests
//
// ****************************************************************
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
System.out.println( + numPowersOf2 + " Is the number of powers of 2 you entered ");
do
{
//print out current power of 2
System.out.println("2^" + exponent + "=" + (2 * (2*exponent)));
//find next power of 2 -- how do you get this from the last one?
nextPowerOf2 = 2 * (numPowersOf2 + 1);
//increment exponent

}
while (exponent<=numPowersOf2);
}
}```
It compiles, but then when I run it it gives me "2^0=0" an infinite amount of time.
any help would be great.

2. ## Re: Powers without Math.pow ???

Java Code:
`nextPowerOf2 = 2 * (numPowersOf2 + 1); // ??`
You can't code this if you don't understand the math. Can you answer for me: How do you calculate 2 to the 3rd power on paper?

3. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: Powers without Math.pow ???

Originally Posted by Fubarable
Java Code:
`nextPowerOf2 = 2 * (numPowersOf2 + 1); // ??`
You can't code this if you don't understand the math. Can you answer for me: How do you calculate 2 to the 3rd power on paper?

2*2*2

4. ## Re: Powers without Math.pow ???

Originally Posted by loftus727
2*2*2
Good, so inside of the loop what do you multiply nextPowerOf2 by?

Also, your while loop checks the value of exponent. Does exponent *change* inside of the loop? If it doesn't, will the loop ever exit?

5. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: Powers without Math.pow ???

Thank you I figured it out, it runs correctly now.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•