Results 1 to 6 of 6
  1. #1
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default 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 2n1?). 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Powers without Math.pow ???

    Quote Originally Posted by Fubarable View Post
    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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Powers without Math.pow ???

    Quote Originally Posted by loftus727 View Post
    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. #5
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Powers without Math.pow ???

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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. math help
    By kormath in forum New To Java
    Replies: 13
    Last Post: 02-17-2013, 12:24 PM
  2. Math help
    By stuffses in forum New To Java
    Replies: 5
    Last Post: 03-09-2012, 02:00 AM
  3. Doing Math
    By nicholas205 in forum New To Java
    Replies: 1
    Last Post: 01-28-2012, 03:18 AM
  4. math and GUI
    By urbanleg in forum AWT / Swing
    Replies: 3
    Last Post: 08-06-2011, 05:05 PM
  5. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 07:23 AM

Posting Permissions

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