Find Power of a Number in Java
In this post, you will learn how to find the power of a number using the Java programming language.
The power (or exponent) of a number says how many times to use the number in a multiplication. The power defines a base number raised to the exponent, where the base number is the factor which is multiplied by itself, and the exponent denotes the number of times the same base number is multiplied.
Power of a Number in Java using for loop
In the given Java program, we are calculating the power of a given number using the for loop. In each iteration, the exponent is decremented by 1, and the result is multiplied by the base exponent number of times.
class CalculatePower {
public static void main(String[] args) {
int base = 5, exponent = 4;
long result = 1;
System.out.println("Value of base: " + base);
System.out.println("Value of exponent: " + exponent);
for (; exponent != 0; --exponent) {
result *= base;
}
System.out.println("Output: " + result);
}
}
Output of the above code:
Value of base: 5
Value of exponent: 4
Output: 625
Power of a Number in Java using while loop
In the given Java program, we are calculating the power of a given number using the while loop. In this, we keep on multiplying the result by base until the exponent becomes zero.
class CalculatePower {
public static void main(String[] args) {
int base = 3, exponent = 4;
long result = 1;
System.out.println("Value of base: " + base);
System.out.println("Value of exponent: " + exponent);
int i=exponent;
while (i != 0)
{
result *= base;
--i;
}
System.out.println(base+"^"+exponent+" = "+result);
}
}
Output of the above code:
Value of base: 3
Value of exponent: 4
3^4 = 81
Power of a Number in Java using pow() function
The Math.pow() function in Java returns the power of a number. This function accepts a number as the first argument and the exponent or power of the number as the second argument.
class CalculatePower {
public static void main(String[] args) {
int base = 10, exponent = 3;
double result = Math.pow(base, exponent);
System.out.println(base+"^"+exponent+" = "+result);
}
}
Output of the above code:
10^3 = 1000.0
Power of a Negative Number in Java
In the given Java program, we have used the pow() method to compute the power of a negative number.
class CalculatePower {
public static void main(String[] args) {
int base = -5, exponent = 3;
double result = Math.pow(base, exponent);
System.out.println(base+"^"+exponent+" = "+result);
}
}
Output of the above code:
-5^3 = -125.0
Power of a Number in Java using Recursion
A recursion function is a function that is called by itself. In the given example, we use the recursion function to get the power of a number. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion.
class CalculatePower {
// Recursive function
static int power(int n, int p)
{
if (p == 0)
return 1;
else
return n * power(n, p - 1);
}
// Driver code
public static void main(String[] args)
{
int base = 6;
int exponent = 3;
System.out.println(base+"^"+exponent+" = "+power(base, exponent));
}
}
Output of the above code:
6^3 = 216
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java