In this post Java Program to Calculate the Power of a Number you will learn how to find the power of a number with and without using pow() function.
Approach 1:
Here, either we can use while loop or for loop. We have core logic is that after each iteration exponent is decremented by 1 and result is multiplied by base exponent number of times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kkjavatutorials.clients; import java.util.Scanner; /** * @author KK JavaTutorials *This java Program is all about How to Calculate the Power of a Number */ public class CalculatePowerOfNumberTest1 { public static void main(String[] args) { Scanner scanner = null; try { //Take base and exponent number from keyboard using Scanner scanner = new Scanner(System.in); System.out.println("Enter base Number:"); //Take either zero,positive or negative base number int base = scanner.nextInt(); //Take either zero or positive exponent number System.out.println("Enter exponent Number:"); int exponent = scanner.nextInt(); int exp =exponent; long result = 1; for (;exponent != 0; --exponent){ result *= base; } System.out.println(base+" to power "+exp+" is:" + result); } catch (Exception e) { e.printStackTrace(); }finally { if(scanner != null) scanner.close(); } } } |
I have tested above programs for few inputs as below. You may test for other input as well
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Enter base Number: 5 Enter exponent Number: 3 5 to power 3 is:125 ------------------------------------------------------------------------------------------------- Enter base Number: -4 Enter exponent Number: 3 -4 to power 3 is:-64 ------------------------------------------------------------------------------------------------- Enter base Number: 0 Enter exponent Number: 2 0 to power 2 is:0 ------------------------------------------------------------------------------------------------- Enter base Number: 10 Enter exponent Number: 0 10 to power 0 is:1 |
Approach 2:
What we have seen above Approach 1 that will not work if you have a negative exponent. For that, you may to use pow() function in Java standard library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kkjavatutorials.clients; import java.util.Scanner; /** * @author KK JavaTutorials *This Java Program is all about How to Calculate the Power of a Number using pow() * function in Java standard library. */ public class CalculatePowerOfNumberTest2 { public static void main(String[] args) { Scanner scanner = null; try { //Take base and exponent number from keyboard using Scanner scanner = new Scanner(System.in); System.out.println("Enter base Number:"); //Take either zero,positive or negative base number int base = scanner.nextInt(); //Take either zero,positive or negative exponent number System.out.println("Enter exponent Number:"); int exponent = scanner.nextInt(); double result = Math.pow(base, exponent); System.out.println(base+" to power "+exponent+" is:" + result); } catch (Exception e) { e.printStackTrace(); }finally { if(scanner != null) scanner.close(); } } } |
I have tested above programs for few inputs as below. You may test for other input as well
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Enter base Number: 2 Enter exponent Number: -3 2 to power -3 is:0.125 --------------------------------------------------------------------------- Enter base Number: 20 Enter exponent Number: 3 20 to power 3 is:8000.0 -------------------------------------------------------------------------- Enter base Number: -6 Enter exponent Number: 3 -6 to power 3 is:-216.0 |
That’s all in this post of Java Program to Calculate the Power of a Number
You May Also Like:
How to swap two numbers with or without temporary variable in java
Java Program to Swap two numbers using Bitwise XOR Operator?
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
Check whether a number is prime or not
Java Program to displaying prime numbers
Fibonacci series using iterative and recursive approach java program
Find largest and second largest number in the given Array
Java program to Remove Duplicate Elements From an Array
Find common elements between two Arrays
Find largest and smallest number in the given Array
Java Program to find duplicate elements in an Array
Count number of words in a string in java
How would you check if a number is even or odd using bit wise operator in Java?
How can you check if the given number is power of 2?
How to check if String is number in java ?
If you have any feedback or suggestion please feel free to drop in blow comment box.