Results 1 to 6 of 6
Thread: Powers without Math.pow ???
- 03-03-2013, 09:07 PM #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:
It compiles, but then when I run it it gives me "2^0=0" an infinite amount of time.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); } }
any help would be great.
-
Re: Powers without Math.pow ???
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?Java Code:nextPowerOf2 = 2 * (numPowersOf2 + 1); // ??
- 03-03-2013, 09:48 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 24
- Rep Power
- 0
-
Re: Powers without Math.pow ???
- 03-03-2013, 09:54 PM #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.
-
Re: Powers without Math.pow ???
Good for you and congrats!
Similar Threads
-
math help
By kormath in forum New To JavaReplies: 13Last Post: 02-17-2013, 11:24 AM -
Math help
By stuffses in forum New To JavaReplies: 5Last Post: 03-09-2012, 01:00 AM -
Doing Math
By nicholas205 in forum New To JavaReplies: 1Last Post: 01-28-2012, 02:18 AM -
math and GUI
By urbanleg in forum AWT / SwingReplies: 3Last Post: 08-06-2011, 04:05 PM -
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12-07-2010, 06:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks