Java Program to Calculate the Power of a Number

By | September 22, 2018

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.

I have tested  above programs for few inputs as below. You may test for other input as well 

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.

I have tested  above programs for few inputs as below. You may test for other input as well 

That’s all in this post of Java Program to Calculate the Power of a Number
If you have any feedback or suggestion please feel free to drop in blow comment box.

 

Leave a Reply

Your email address will not be published. Required fields are marked *