Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exponents without math.pow

807598Oct 30 2006 — edited Oct 30 2006
Trying to Complete this program without using math.pow I dont know how to make it compute the exponents and ahve them printed individually.
// ****************************************************************
//   PowersOf2.java
//
//   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;            //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 + " powers of 2 will be printed");

	//initialize exponent -- the first thing printed is 2 to the what?
	exponent = 0;

	  while (nextPowerOf2 <= numPowersOf2)
        {
	//print out current power of 2
	System.out.println ("2^" + nextPowerOf2 + "= " (2^nextPowerOf2);
        
	//find next power of 2 -- how do you get this from the last one?
	nextPowerOf2 = nextPowerOf2 + 1;
        
	//increment exponent
	exponent = exponent + 1;

	}
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 27 2006
Added on Oct 30 2006
3 comments
206 views