Java Program To Find Power Of Number Using While Loop
Chapter:
Interview Programs
Last Updated:
30-08-2017 09:54:22 UTC
Program:
/* ............... START ............... */
public class JavaPowerOfNumberUsingWhileLoop {
public static void main(String[] args) {
int base = 2, exponent = 3;
long result = 1;
while (exponent != 0) {
result *= base;
--exponent;
}
System.out.println("Answer = " + result);
}
}
/* ............... END ............... */
Output
Notes:
-
In this program, base and exponent are assigned values 2 and 3 respectively.
- In the next step we keep on multiplying result by base until exponent becomes zero.
- Result is : 1 * 2 * 2 * 2 = 8
Tags
Program To Fin Power Of Number Using While Loop, Java, Interview