How to calculate natural and base 10 logarithm of a number through a Java program ?.

Program to demonstrate how to calculate natural and base 10 logarithm of a number in Java.

package com.hubberspot.code;
import java.util.Scanner;
public class LogarithmCalculation {
public static void main(String[] args) {
// Create a Scanner object which will read
// values from the console which user enters
Scanner scanner = new Scanner(System.in);
// Getting input from user from the console
System.out.println("Enter value of number ");
// Calling nextDouble method of scanner for
// taking a double value from user and storing
// it in number variable
double number = scanner.nextDouble();
System.out.println();
System.out.println("Calculating base 10 logarithm of a number ... ");
// In order to calculate base 10 logarithm of a number
// we use Math class log10() static method which takes in a
// number and returns back the base 10 logarithm of a number
double result = Math.log10(number);
// printing the result on the console
System.out.println("Base 10 Logarithm of " + number + " is : " + result);
System.out.println();
System.out.println("Calculating Natural logarithm of a number ... ");
// In order to calculate natural logarithm of a number
// we use Math class log() static method which takes in a
// number and returns back the natural logarithm of a number
result = Math.log(number);
// printing the result on the console
System.out.println("Natural Logarithm of " + number + " is : " + result);
}
}

Output of the program :

Program to demonstrate how to calculate natural and base 10 logarithm of a number in Java.

package com.hubberspot.code;
import java.util.Scanner;
public class LogarithmCalculation {
public static void main(String[] args) {
// Create a Scanner object which will read
// values from the console which user enters
Scanner scanner = new Scanner(System.in);
// Getting input from user from the console
System.out.println("Enter value of number ");
// Calling nextDouble method of scanner for
// taking a double value from user and storing
// it in number variable
double number = scanner.nextDouble();
System.out.println();
System.out.println("Calculating base 10 logarithm of a number ... ");
// In order to calculate base 10 logarithm of a number
// we use Math class log10() static method which takes in a
// number and returns back the base 10 logarithm of a number
double result = Math.log10(number);
// printing the result on the console
System.out.println("Base 10 Logarithm of " + number + " is : " + result);
System.out.println();
System.out.println("Calculating Natural logarithm of a number ... ");
// In order to calculate natural logarithm of a number
// we use Math class log() static method which takes in a
// number and returns back the natural logarithm of a number
result = Math.log(number);
// printing the result on the console
System.out.println("Natural Logarithm of " + number + " is : " + result);
}
}