Click to See Complete Forum and Search --> : Can somebody help me finish this
tmlucky14
Oct 25th, 2006, 02:15 PM
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++;
}
}
}
ComputerJy
Oct 25th, 2006, 07:46 PM
I guess this is a little bit easier and works quicker
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 ) ) ;
}
}
}
oceanebelle
Oct 25th, 2006, 09:55 PM
He did say to not use Math.pow
:)
lunchboxtheman
Oct 25th, 2006, 10:00 PM
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.
oceanebelle
Oct 25th, 2006, 10:18 PM
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.
oceanebelle
Oct 25th, 2006, 10:19 PM
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:
ComputerJy
Oct 25th, 2006, 11:07 PM
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.
for ( int i = 0 ; i < max ; i++ )
Or you can rewrite the pow methodimport 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 ;
}
}
ComputerJy
Oct 25th, 2006, 11:09 PM
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
oceanebelle
Oct 25th, 2006, 11:30 PM
WOW, you did good research about this
I bet he does his own homework. ;)
AgonySorrow
Oct 17th, 2009, 04:59 PM
// ****************************************************************
// 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++;
}
}
}
manavo11
Oct 25th, 2009, 08:48 AM
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).
ken_coffee
Nov 11th, 2009, 06:52 AM
could you try this code
int y = 0;
for(int x = 0;x < input-1;x++){
y = 2 ^ x;
}
^^
just trying to help
^^
ComputerJy
Nov 11th, 2009, 07:00 AM
looooooooooool. did you try this code before posting it!
ken_coffee
Nov 11th, 2009, 07:03 AM
no ^^
but based on what example he posted i think its correct ??
ComputerJy
Nov 11th, 2009, 07:09 AM
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.