Exponential Java

The problem of Exponential Java

In this blog, we will check whether java has an exponential operator or not also known as exponential java. The exponential returns the number of times a particular number gets multiplied by itself. 

 

 

Solution

After a good amount of searching one can come to the conclusion that there is no particular operator in java for the purpose of exponential computation, unlike python. However, there are many other ways by which one can calculate the exponential number for any given input number. 

Java has a special function called power in the math package that allows users to calculate the power of any process. And the same syntax is as follows:

 

Math.pow(number, number of times number should be multiplied)

 

The below example is for the same:

Math.pow(5,2) 

 

The output of the above function is 25.

Apart from this, another way to do the same is by using the concept of for loop. In the for loop, the number gets multiplied by itself by the number of power times. The procedure goes on until we get the desired results. The code for the same is shown below:

 

 

public static double myPow(double a, int b){

double res =1;

for (int i = 0; i < b; i++) {

res *= a;
}

return res;

}

 

 

Another way by which one can solve the same problem is by creating a function to calculate the exponential of any number. The corresponding code is displayed below:

 

 

public static double power(double value, double p) {

if (p <= 0)

return 1;

return value *

power(value, p - 1);

}

 

 

Also Read: rmarkdown figure auto-adjust HTML

 

 

Share this post

Leave a Reply

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