Java Program To Displays Powers of 2 Example
Chapter:
Math Class
Last Updated:
14-06-2016 13:56:52 UTC
Program:
/* ............... START ............... */
public class JavaPowerOfTwoExample {
public static void main(String[] args) {
int n = 8;
int i = 0;
int power = 1;
System.out.println("Powers of 2 that are less than 2^" + n);
while (i <= n) {
System.out.println("2^" + i + " = " + power);
power = power * 2;
i++;
}
}
}
/* ............... END ............... */
Output
Powers of 2 that are less than 2^8
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
Tags
Program Displays Powers of 2, Java, Math