# How to do Exponents in Java

Well, Java provides many functions as well as classes for our real-life problems. This question always comes to programmers who are done with some core Java concepts or solving some Mathematical problems or expressions.

So, let’s start with our simple example. Since childhood, we have learned many mathematical expressions that are still solving our real-life problems and one was ‘Exponents’ from those expressions. Calculating squares, cubes, and so on and so forth of some particular value called an Exponents. For example 5^2 = 25 (here 5 is the base and 2 is its exponent).

In Java, there are two possible ways to do exponents:

- With the help of function ‘Math.pow(base, exponent), which is present in the ‘java.lang’ package, can be used by importing it.
- By iterating through ‘for-loop ‘.

Let’s go through both of the above.

### Math.pow(base,exp) function in Java

Now, this function is in the default package of Java which is ‘java.lang’. We don’t need to import it manually. It is the default package of Java that is imported at the time of class creation. So we can use functions without importing the ‘java.lang’ package.

This function accepts two parameters. The first one will be our base (the value we are calculating for) and the second one is our exponent (the value count which is the limit of iteration). For example, 5^2 = 25, here we are calculating power for value 5 (base) and limit of iteration which is repeating base value is 2 (exponent).

This function will return value in double data type form by default. If we want our return value in some other data type then we can use typecasting. We will also discuss that.

**Algorithm to do Exponents in Java**

- Create a class with the appropriate class name. the class name should not be keywords in Java.
- Create a main method in Java.
- Instantiate a variable with the data type you want.
- Assign it with Math.pow(base_value , exponent_value)
- With the help of the print function, you can print that output in the console window.

Now let’s move one by one to the code.

package com.first; public class ExponentDemo{ public static void main(String[] args){ // if we do not do type casting then it will return answer in double data type by default // Code with type-casting int ans = (int)Math.pow(5,2); System.out.println(ans); } }

*output: 25*

The code given above was with type-casting. We are storing our exponent result in an integer and changing the data type of that variable from double to an integer. This is how with typecasting and with Java function, we can find an exponent of a particular value.

package com.first; public class Practice { public static void main(String[] args) { // without type-casting. double result = Math.pow(5, 2); System.out.println(result); } }

*output: 25.0*

If you store results in an integer without changing the data type of result, then you will get a compilation error.

### By iterating through ‘ for – loop ‘

We have seen the calculation of exponents through function. Now, will look at how we can calculate exponent without using functions. For that, we will use the method in the same class, and in that method, we will iterate through for loop.

**Algorithm**

- Create a class with a className that has no java keywords in it.
- Create a static method that we can define in the main method (which is also a static method) without creating an object and accept two parameters, base, and exponent.
- The base parameter can be of double or int but the exponent parameter must be of integer because we are iterating through a loop and the loop can’t iterate via double type value.
- Return the result of iteration.
- Define the main method and initialize double or int variable with the assignment of the method we have declared in the same class.
- Pass arguments.
- Print results with a print function.

package com.first; public class Practice { public static double pow(double base , int exp) { double res = 1; for(int i=0;i<exp;i++) res *= base; return res; } public static void main(String[] args) { double result = pow(3, 3); System.out.println(result); } }

*output: 27.0*

In the above code, we have declared another static method in the same class. The property of the static method is – it can be accessed only in another static method without creating any object for that class. So, we can access our static method directly to the main class. Bypassing specific parameters to method from the main class, the base is multiplying itself till it reaches the limit i.e exponent through for-loop.

You can learn more about typecasting from below website

Casting and Conversion in Java

That’s all in this tutorial. Hope it will be helpful for your problems.

## Leave a Reply