PowersOfTwo.java
This is the syntax highlighted version of PowersOfTwo.java
from 2.3 Conditionals, Loops of
Introduction to Computer Science by
Robert Sedgewick and Kevin Wayne.
/*************************************************************************
* Compilation: javac PowersOfTwo.java
* Execution: java PowersOfTwo
*
* Prints out the first 31 powers of 2: 2^0, 2^1, ..., 2^30
* using a while loop.
*
* % java PowersOfTwo
* 0 1
* 1 2
* 2 4
* 3 8
* 4 16
* 5 32
* ...
* 29 536870912
* 30 1073741824
*
*************************************************************************/
public class PowersOfTwo {
public static void main(String[] args) {
int i = 0;
int N = 1;
while (i <= 30) {
System.out.println(i + " " + N);
i = i + 1;
N = 2 * N;
}
}
}
Last updated: Sat Sep 18 18:50:14 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.