In this Java program, you’ll learn how to calculate the power of a number. This post explains various ways to calculate the power of a number with and without using pow()
function.
Here are examples to calculate the power of a number in Java.
In this program, you will see how to calculate the power of a number using a while
loop.
//Java Program to Calculate power of a number using a While Loop
import java.util.Scanner;
public class JavaPrograms {
public static void main(String[] args) {
long result = 1;
Scanner sc = new Scanner(System.in);
// Base Number - To calculate power of a number
System.out.println("Enter a Base Number: ");
int base = sc.nextInt();
// Power of a Number - no. of times number to be multiplied
System.out.println("Enter a Exponent: ");
int exponent = sc.nextInt();
int power = exponent;
// closing scanner class(not compulsory, but good programming practice)
sc.close();
while (exponent != 0) {
result *= base;
--exponent;
}
System.out.println("Output of " + base + " to the power " + power + " = " + result);
}
}
Enter a Base Number:
5
Enter a Exponent:
3
Output of 5 to the power 3 = 125
In this program, you will see how to calculate the power of a number using a for
loop.
//Java Program to Calculate power of a number using a For Loop
import java.util.Scanner;
public class JavaPrograms {
public static void main(String[] args) {
long result = 1;
Scanner sc = new Scanner(System.in);
// Base Number - To calculate power of a number
System.out.println("Enter a Base Number: ");
int base = sc.nextInt();
// Power of a Number - no. of times number to be multiplied
System.out.println("Enter a Exponent: ");
int exponent = sc.nextInt();
int power = exponent;
// closing scanner class(not compulsory, but good programming practice)
sc.close();
for (; exponent != 0; --exponent) {
result *= base;
}
System.out.println("Output of " + base + " to the power " + power + " = " + result);
}
}
Enter a Base Number:
5
Enter a Exponent:
2
Output of 5 to the power 2 = 25
In this program, you will see how to calculate the power of a number using a Math.
pow()
function.
//Java Program to Calculate Power of a Number using a pow() Function
import java.util.Scanner;
public class JavaPrograms {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Base Number - To calculate power of a number
System.out.println("Enter a Base Number: ");
int base = sc.nextInt();
// Power of a Number - no. of times number to be multiplied
System.out.println("Enter a Exponent: ");
int exponent = sc.nextInt();
int power = exponent;
// closing scanner class(not compulsory, but good programming practice)
sc.close();
double result = Math.pow(base, exponent);
System.out.println("Output of " + base + " to the power " + power + " = " + result);
}
}
Enter a Base Number:
3
Enter a Exponent:
4
Output of 3 to the power 4 = 81.0
In this program, you will see how to calculate the power of a negative number using a Math.
pow()
function.
//Java Program to Calculate Power of a Negative Number
import java.util.Scanner;
public class JavaPrograms {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Base Number - To calculate power of a number
System.out.println("Enter a Base Number: ");
int base = sc.nextInt();
// Power of a Number - no. of times number to be multiplied
System.out.println("Enter a Exponent: ");
int exponent = sc.nextInt();
int power = exponent;
// closing scanner class(not compulsory, but good programming practice)
sc.close();
double result = Math.pow(base, exponent);
System.out.println("Output of " + base + " to the power " + power + " = " + result);
}
}
Enter a Base Number:
-5
Enter a Exponent:
3
Output of -5 to the power 3 = -125.0
Comments