Can somebody help me finish this
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 20.
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
// ****************************************************************
// 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("Here are the first " + numPowersOf2 + " powers of 2: ");
//initialize exponent -- the first thing printed is 2 to the what?
exponent = 0;
while (exponent < numPowersOf2);
{
//print out current power of 2
System.out.println("2^" + -- exponent + " = " + nextPowerOf2);
//find next power of 2 -- how do you get this from the last one?
nextPowerOf2 = nextPowerOf2 * 2;
//increment exponent
exponent++;
}
}
}
Re: Can somebody help me finish this
I guess this is a little bit easier and works quicker
Code:
import java.util.* ;
public class PowersOf2
{
public static void main ( String args[] )
{
Scanner s = new Scanner( System.in ) ;
System.out.println( "How many powers of 2 would you like printed?" ) ;
int max = s.nextInt() ;
for ( int i = 0 ; i < max ; i++ )
{
System.out.println( Math.pow( 2, i ) ) ;
}
}
}
Re: Can somebody help me finish this
He did say to not use Math.pow
:)
Re: Can somebody help me finish this
Again, a direct excercise from the Lab Manual to "Java Software Solutions" by Lewis, Loftus, and Cocking. This was copied word for word, from page 56 to be exact. If you have specific questions about something, we'll answer, otherwise, do your own homework.
Re: Can somebody help me finish this
Code:
import java.util.* ;
public class PowersOf2 {
public static void main ( String args[] )
{
Scanner s = new Scanner( System.in ) ;
int pow = 0;
System.out.println( "How many powers of 2 would you like printed?" ) ;
int max = s.nextInt() ;
for ( int i = 0 ; i <= max ; i++ )
{
pow += (pow == 0)? 1: pow;
System.out.println( pow ) ;
}
}
}
i did not test this though, and i think there are areas where it can be improved.
Re: Can somebody help me finish this
Quote:
Originally Posted by lunchboxtheman
Again, a direct excercise from the Lab Manual to "Java Software Solutions" by Lewis, Loftus, and Cocking. This was copied word for word, from page 56 to be exact. If you have specific questions about something, we'll answer, otherwise, do your own homework.
Hehehe, I had nothing better to do, so I figured to answer with c0d.
He was having difficulties with the logic and approach I guess. :wave:
Re: Can somebody help me finish this
Quote:
Originally Posted by oceanebelle
Code:
import java.util.* ;
public class PowersOf2 {
public static void main ( String args[] )
{
Scanner s = new Scanner( System.in ) ;
int pow = 0;
System.out.println( "How many powers of 2 would you like printed?" ) ;
int max = s.nextInt() ;
for ( int i = 0 ; i <= max ; i++ )
{
pow += (pow == 0)? 1: pow;
System.out.println( pow ) ;
}
}
}
i did not test this though, and i think there are areas where it can be improved.
Code:
for ( int i = 0 ; i < max ; i++ )
Or you can rewrite the pow method
Code:
import java.util.* ;
public class PowersOf2
{
public static void main ( String args[] )
{
Scanner s = new Scanner( System.in ) ;
System.out.println( "How many powers of 2 would you like printed?" ) ;
int max = s.nextInt() ;
for ( int i = 0 ; i < max ; i++ )
{
System.out.println( pow( 2, i ) ) ;
}
}
public static long pow ( int a, int b )
{
long ret = 1 ;
for ( int i = 0 ; i < b ; i++ )
{
ret *= a ;
}
return ret ;
}
}
Re: Can somebody help me finish this
Quote:
Originally Posted by lunchboxtheman
Again, a direct excercise from the Lab Manual to "Java Software Solutions" by Lewis, Loftus, and Cocking. This was copied word for word, from page 56 to be exact. If you have specific questions about something, we'll answer, otherwise, do your own homework.
WOW, you did good research about this
Re: Can somebody help me finish this
Quote:
Originally Posted by ComputerJy
WOW, you did good research about this
I bet he does his own homework. ;)
Re: Can somebody help me finish this
// ****************************************************************
// 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;
int pow=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("You requested to print to the " + numPowersOf2+ "th power");
exponent = 0;
while ( exponent < numPowersOf2 )
{
//print out current power of 2
System.out.println("2^"+ pow++ + " = " + nextPowerOf2);
//find next power of 2 -- how do you get this from the last one?
nextPowerOf2 = nextPowerOf2 * 2;
//increment exponent
exponent++;
}
}
}
Re: Can somebody help me finish this
One thing I notice is that the pow and exponent variables appear to be doing exactly the same thing. As for calculating the next value based on the previous one, I think nextPowerOf2 = nextPowerOf2*2 should work. If not, you can always calculate the actual value based on the value of pow/exponent (instead of calculating based on the previous value).
Re: Can somebody help me finish this
could you try this code
int y = 0;
for(int x = 0;x < input-1;x++){
y = 2 ^ x;
}
^^
just trying to help
^^
Re: Can somebody help me finish this
looooooooooool. did you try this code before posting it!
Re: Can somebody help me finish this
no ^^
but based on what example he posted i think its correct ??
Re: Can somebody help me finish this
Well, 1st of HE posted the question 3 years ago. He might have graduated by now and is no longer doing java :D
2nd of all your code does nothing related to his question. In case you're a bit lost this is the Java forum not VB forum