# Java Code For log() – 4 Simple Ways | Java Codes

Java Program To Calculate Log() – In this article, we will brief in on all the possible ways to log in Java. Log Java code has been written in all possible ways here. If you have any queries leave a comment here.

• Using Standard Class
• Using Scanner Class
• Using Predefined Method
• Using Static Method
• Using Separate Class

Logarithm, as we all know, is the inverse function to the exponentiation. A logarithm of a number given is equal to the exponent to which another number, the base is raised to the power of. As you can see in the image, the quantity in the left-hand side is referred to as log a to the base “b” or simply log a.

The base part is excluded when the value generally is 10 or e.

Logarithms with base 10 are represented as log, which is known as Common Logarithms. Similarly, logarithms with base e are represented as ln, known as Natural Logarithms.

Thus, the multiple ways to calculate log in Java Programming are as follows:

## Log() Java –  Standard Method

The standard approach to solving this problem is to first take input in the code itself. The input for our given problem is an integer number.

To find log in java, to make things simpler, the Math package in Java already has a built in method named log for this.

If you need log for base 10 you can use the Math.log10() for base e, it is Math.log().

These methods are already predefined so, to get the desired output you just need to directly call this function and display in the output screen.

Output:

Using Scanner Class

Instead of giving input in the code, using Scanner class in Java, we can read input at runtime itself. So, making use of this for our problem, we read the inputs – number whose log has to be found (n) and the base for log (b).

As log is not fixed,it could be anything other than 10 or e. Under such a scenario the built in methods for log can’t be used.

So, the logic here is that, we take another variable (c). Until the number n remains greater than 1, we keep dividing the number (n) with the base (b) and increment variable (c) by 1 every time.

After coming out of this iterative loop, we add the number (n) after iteration to the variable (c).

Our output for log n base b is approximately now equal to (c-1). Just to give you clarity, the output value here is only the approximate value and not the exact value.

Output:

### Log Java – Using Predefined Method

As we have seen in the first method of this problem, there are predefined methods in Math package to find the log of a number given that its base is either 10 or e.

For base 10, it is Math.log10() whereas for base e, it is Math.log() method.

But, in the standard method, we took the input in the code itself which is not an efficient way for writing a code.

This is because, if it is written in the code itself then, for every testcase, we must go and make changes in the code. So instead, we make use of the Scanner class that is discussed above to read the input number at runtime.

Output:

Using Static Method

This method is being made use of in order to increase the readability of the code as well as to make the logic reusable.

The logic remains the same here, we read the two inputs- the number and the base.

Then the method discussed under the scanner class is made use of. The resultant value is the subtracted by 1 same as above which is the log of the number for the given base.

The difference here is that, the code it split into parts.

The input output operations are taken care of by the main method while the logic for calculating the log is taken care of by another static method (logCal) by passing the input values as its arguments.

The linking between these two is that, in the main method, the other static method (logCal) is called which returns our desired value.

Output: