|
-
Oct 25th, 2006, 01:15 PM
#1
Thread Starter
Junior Member
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++;
}
}
}
-
Oct 25th, 2006, 06:46 PM
#2
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 ) ) ;
}
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 25th, 2006, 08:55 PM
#3
Re: Can somebody help me finish this
He did say to not use Math.pow
-
Oct 25th, 2006, 09:00 PM
#4
Lively Member
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.
Last edited by lunchboxtheman; Oct 25th, 2006 at 09:07 PM.
-
Oct 25th, 2006, 09:18 PM
#5
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.
-
Oct 25th, 2006, 09:19 PM
#6
Re: Can somebody help me finish this
 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.
-
Oct 25th, 2006, 10:07 PM
#7
Re: Can somebody help me finish this
 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 ;
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 25th, 2006, 10:09 PM
#8
Re: Can somebody help me finish this
 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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 25th, 2006, 10:30 PM
#9
Re: Can somebody help me finish this
 Originally Posted by ComputerJy
WOW, you did good research about this
I bet he does his own homework.
-
Oct 17th, 2009, 03:59 PM
#10
New Member
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++;
}
}
}
-
Oct 25th, 2009, 07:48 AM
#11
-
Nov 11th, 2009, 06:52 AM
#12
Lively Member
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
^^
-
Nov 11th, 2009, 07:00 AM
#13
Re: Can somebody help me finish this
looooooooooool. did you try this code before posting it!
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Nov 11th, 2009, 07:03 AM
#14
Lively Member
Re: Can somebody help me finish this
no ^^
but based on what example he posted i think its correct ??
-
Nov 11th, 2009, 07:09 AM
#15
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 
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|