Signup/Sign In

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:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare two variables for the base and exponent.
  4. Ask the user to initialize both variables.
  5. Use a while loop to calculate the power of a number.
  6. Print the calculated value.
  7. 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:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare two variables for the base and exponent.
  4. Ask the user to initialize both variables.
  5. Use a for loop to calculate the power of a number.
  6. Print the calculated value.
  7. 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:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare two variables.
  4. Ask the user to initialize the variables.
  5. Use Math.pow() to calculate the power of the number.
  6. Print the value of the power of the number.
  7. 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



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.