Pages

Tuesday, November 10, 2020

Java Program to Calculate the Power of a Number With Examples

1. Overview

In this tutorial, you are going to learn how to find the power of a number without using Math.pow() method.

But, the power of a number can be solved with the help of for loop and while loops.

Example:

Input: Base = 4, Exponent : 3
Output: 63

Finding the power is to multiply the base with exponent value that means here base is 4 and exponent is 3.
So, power calculation is 4 * 4 * 4 = 64.

Java Program to Calculate the Power of a Number With Examples

2. Example 1 - Power Of A Number Using For Loop


In the below program, first created two integer type variables base and exponent.

Next, run through the for loop for exponent times and multiply the base value those many times.
public class NumberPowerForLoopExample {

	public static void main(String[] args) {

		// create two variables to store the base and exponenet values
		int base = 4;
		int exponent = 3;

		// printing the input values
		System.out.println("Base : " + base + ", Exponent : " + exponent);

		// a result variable to store the output
		int result = 1;

		// Calculating the power using for loop
		for (int i = 1; i <= exponent; i++) {
			result = result * base;
		}

		// Printing the output
		System.out.println("Result : " + result);

	}

}
Output:
Base : 4, Exponent : 3
Result : 64

3. Example 2 - Power Of A Number Using While Loop


Next, run the same logic using while loop until the exponent is 0.

Look at the below example and output.
public class NumberPowerWhileLoopExample {

	public static void main(String[] args) {

		// create two variables to store the base and exponenet values
		int base = 5;
		int exponent = 4;

		// printing the input values
		System.out.println("Base : " + base + ", Exponent : " + exponent);

		// a result variable to store the output
		int result = 1;

		// Calculating the power using while loop
		while (exponent > 0) {
			result = result * base;
			exponent--;
		}

		// Printing the output
		System.out.println("Result : " + result);

	}

}
Output:
Base : 5, Exponent : 4
Result : 625

4. Example 3 - Power Of A Number Using Math.pow() Method


In the last approach, you can use the java Math API builtin method pow(base, exponent) which does the calculation of power of a given number for the given exponent.

This can be done using recursive approach
package com.javaprogramto.programs.numbers;

public class NumberPowerMathPowExample {

	public static void main(String[] args) {

		// create two variables to store the base and exponenet values
		int base = 5;
		int exponent = 7;

		// printing the input values
		System.out.println("Base : " + base + ", Exponent : " + exponent);

		// a result variable to store the output
		double result = 1;

		// Calculating the power using Math.pow() method
		result = Math.pow(base, exponent);

		// Printing the output
		System.out.println("Result : " + result);
	}
}

Output:
Base : 5, Exponent : 7
Result : 78125.0

5. Conclusion


In this article, you've seen the three ways to find the power of a number.

All examples are over GitHub.



No comments:

Post a Comment

Please do not add any spam links in the comments section.