Java Program to Calculate the Power of a Number
In this tutorial, we will learn how to find the power of a number in java. The power of a number is defined as the value which is obtained by multiplying the base value n number of times where n is the exponent value. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java.
Input: Enter the base value: 2
Enter the exponent value: 4
Output: 2 raised to the power 4 is 16.0
The above problem can be solved in the following ways:
Approach 1: Using a While Loop
Approach 2: Using a For Loop
Approach 3: Using pow()
Let us look at each of these methods separately
Program 1: Java Program to Calculate the Power of a Number
In this program, we will see how to calculate the power of a number using a while loop.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare two variables for the base and exponent.
- Ask the user to initialize both variables.
- Use a while loop to calculate the power of a number.
- Print the calculated value.
- Stop
Below is the code for the same.
//Java Program to Calculate the Power of a number
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base value: ");
int base = sc.nextInt();
System.out.println("Enter the exponent value: ");
int exp = sc.nextInt();
long result = 1;
System.out.print(base+ " raised to the power "+ exp+" is: ");
while (exp != 0)
{
result *= base;
--exp;
}
System.out.println(result);
}
}
Enter the base value: 2
Enter the exponent value: 3
2 raised to the power 3 is: 8
Program 2: Java Program to Calculate the Power of a Number
In this program, we will see how to calculate the power of a number using a for loop.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare two variables for the base and exponent.
- Ask the user to initialize both variables.
- Use a for loop to calculate the power of a number.
- Print the calculated value.
- Stop
Below is the code for the same.
//Java Program to Calculate the Power of a number
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base value: ");
int base = sc.nextInt();
System.out.println("Enter the exponent value: ");
int exp = sc.nextInt();
long result = 1;
System.out.print(base+ " raised to the power "+ exp+" is: ");
for (;exp != 0; --exp)
{
result *= base;
}
System.out.println(result);
}
}
Enter the base value: 3
Enter the exponent value: 3
3 raised to the power 3 is: 27
Program 3: Java Program to Calculate the Power of a Number
In this program, we will see how to calculate the power of a number using pow().
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare two variables.
- Ask the user to initialize the variables.
- Use Math.pow() to calculate the power of the number.
- Print the value of the power of the number.
- Stop
Below is the code for the same.
//Java Program to Calculate the Power of a number
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base value: ");
int base = sc.nextInt();
System.out.println("Enter the exponent value: ");
int exp = sc.nextInt();
System.out.print(base+ " raised to the power "+ exp+" is: ");
double result = Math.pow(base, exp);
System.out.println(result);
}
}
Enter the base value: 8
Enter the exponent value: 2
8 raised to the power 2 is: 64.0