Logarithm in java – Java Program to Find Logarithm of a Number

Logarithm in java: In the previous article, we have seen Java Program to find Quotient and Remainder of a Number

In this article we are going to see how to find logarithm of a number using java programming language.

Java Program to Find Logarithm of a Number

Java logarithm: In Java we have inbuilt Math.log() method in java.lang.Math class which can be used to find the logarithm of a number.

Let’s see the program to understand it more clearly.

Method-1: Java Program to Find Logarithm of a Number By Using Math.log() Method (Static Input Value)

Approach:

  1. Take an integer variable initialized with an integer value.
  2. Then use Math.log() method to find the logen and Math.log10() to find log10.

Program:

import java.util.Scanner;

class Main
{
    public static void main(String args[]) 
    {
        //a number declared
        int n = 15;
        System.out.println("Logarithm of " + n + " is " + Math.log(n));
        System.out.println("Logarithm10 of " + n + " is " + Math.log10(n));
    }
}
Output:

Logarithm of 15 is 2.70805020110221
Logarithm10 of 15 is 1.1760912590556813

Method-2: Java Program to Find Logarithm of a Number By Using Math.log() Method (User Input Value)

Approach:

  1. Create scanner class object.
  2. Take user input for the number.
  3. Use Math.log() method to find the logen and Math.log10() to find log10

Program:

import java.util.Scanner;

class Main
{
    public static void main(String args[]) 
    {
        //Scanner class object created
        Scanner sc = new Scanner(System.in);
        //Taking a number input from user
        System.out.print("Enter the number: ");
        int n = sc.nextInt();
        System.out.println("Logarithm of " + n + " is " + Math.log(n));
        System.out.println("Logarithm10 of " + n + " is " + Math.log10(n));
    }
}
Output:

Enter the number: 5
Logarithm of 5 is 1.6094379124341003
Logarithm10 of 5 is 0.6989700043360189

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Programs: