Introduction
Calculating the power of a number is a common task in mathematics and programming. In this context, raising a base number to an exponent means multiplying the base by itself as many times as indicated by the exponent. This guide will walk you through writing a Java program that calculates the power of a given base number raised to a specified exponent.
Problem Statement
Create a Java program that:
- Prompts the user to enter a base number and an exponent.
- Calculates the power of the base number raised to the exponent.
- Displays the result.
Example:
- Input: Base: 2,Exponent: 3
- Output: "2 raised to the power of 3 is 8"
Solution Steps
- Read the Base and Exponent: Use the Scannerclass to take the base number and the exponent as input from the user.
- Calculate the Power: Implement logic to calculate the power using either a loop or the Math.pow()method.
- Display the Result: Print the result of the base raised to the power of the exponent.
Java Program Using Math.pow()
// Java Program to Calculate the Power of a Number
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
public class PowerCalculator {
    public static void main(String[] args) {
        // Step 1: Read the base and exponent from the user
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter the base number: ");
            double base = scanner.nextDouble();
            
            System.out.print("Enter the exponent: ");
            double exponent = scanner.nextDouble();
            
            // Step 2: Calculate the power using Math.pow()
            double result = Math.pow(base, exponent);
            
            // Step 3: Display the result
            System.out.println(base + " raised to the power of " + exponent + " is " + result);
        }
    }
}
Java Program Using a Loop
// Java Program to Calculate the Power of a Number Using a Loop
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
public class PowerCalculatorLoop {
    public static void main(String[] args) {
        // Step 1: Read the base and exponent from the user
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter the base number: ");
            int base = scanner.nextInt();
            
            System.out.print("Enter the exponent: ");
            int exponent = scanner.nextInt();
            
            // Step 2: Calculate the power using a loop
            int result = 1;
            for (int i = 1; i <= exponent; i++) {
                result *= base;
            }
            
            // Step 3: Display the result
            System.out.println(base + " raised to the power of " + exponent + " is " + result);
        }
    }
}
Explanation
Step 1: Read the Base and Exponent
- The Scannerclass is used to read the base and exponent values as inputs from the user. ThenextDouble()method is used for floating-point numbers, whilenextInt()is used for integers.
Step 2: Calculate the Power
- Using Math.pow(): TheMath.pow()method is a built-in function in Java that calculates the power of a number. It takes two arguments: the base and the exponent.
- Using a Loop: A loop is used to multiply the base by itself as many times as indicated by the exponent. This method works well for integer values.
Step 3: Display the Result
- The program prints the result of the base raised to the power of the exponent using System.out.println().
Output Example
Example 1: Using Math.pow()
Enter the base number: 2
Enter the exponent: 3
2.0 raised to the power of 3.0 is 8.0
Example 2: Using a Loop
Enter the base number: 2
Enter the exponent: 3
2 raised to the power of 3 is 8
Conclusion
These Java programs demonstrate two different methods for calculating the power of a number: using the built-in Math.pow() method and using a loop for manual calculation. Both approaches cover essential concepts such as loops, arithmetic operations, and user input handling, making them valuable exercises for beginners learning Java programming.