Java Program To Find Power Of Number Using For Loop
Chapter:
Miscellaneous
Last Updated:
29-08-2017 15:26:37 UTC
Program:
/* ............... START ............... */
public class JavaPowerOfNumberUsingForLoop {
public static void main(String[] args) {
int base = 2, exponent = 4;
long result = 1;
for (; exponent != 0; --exponent) {
result *= base;
}
System.out.println("Answer = " + result);
}
}
/* ............... END ............... */
Output
Notes:
-
Here we used for loop to find the power of a number.
- After each iteration, exponent is decremented by 1, and result is multiplied by base exponent number of times.
Tags
Program To Find Power Of Number Using For Loop, Java, Interview