Get the base 10 logarithm of a value in Java

Java 8Object Oriented ProgrammingProgramming

To get the base 10 logarithm of value in Java, we use the java.lang.Math.log10() method. This method returns the log of value to the base 10 in the form of a double. If a number is 10n, then the result is n. If the value passed is NaN or negative, the result is NaN. If the value passed is positive infinity, then the value returned is positive infinity. When the parameter is positive zero or negative zero, then negative infinity is returned.

Declaration −The java.lang.Math.log10() method is declared as follows −

public static double log10(double a)

where a is a value whose base 10 logarithm is to be found.

Let us see a program to get the base 10 logarithm of a number in Java using the Math.log10() method.

Example

 Live Demo

import java.lang.Math;
public class Example {
   public static void main(String[] args) {
      // declaring and initializing two double values
      double x = 300.0;
      double y = 100000;
      // printing their base 10 logarithms
      System.out.println("Base 10 logarithm of "+ x + " is " + Math.log10(x));
      System.out.println("Base 10 logarithm of "+ y + " is " + Math.log10(y));
   }
}

Output

Base 10 logarithm of 300.0 is 2.4771212547196626
Base 10 logarithm of 100000.0 is 5.0
raja
Published on 07-Jan-2019 11:55:31
Advertisements