Mathematical Functions in Java Programming
Mathematical Functions
In this lesson, we will understand what is Mathematical Function, and with the help of examples, we will see how to use mathematical functions in the Java program.
List of Java Mathematical Functions
Java Mathematical functions are predefined functions which accept values and return the result. To use mathematical functions, we have to use Math class in our program.
With the help of mathematical functions we can easily solve a complex equation in our program, for example, if we want to find the square root of a number, then we can use sqrt() mathematical function to find the square root of a given number.
Let's see all the important mathematical functions available in Java in more details with examples.
abs() Function
The abs() function returns the absolute value of a number. The number can be an integer, long, float or double value. Here Absolute value means number without negative sign. The absolute value of a number is always positive.
Syntax of abs() function
int abs(int num); // Returns the absolute value of an integer data type number
long abs(long num); // Returns the absolute value of a long data type number
float abs(float num); // Returns the absolute value of a float data type number
double abs(double num); // Returns the absolute value of a double data type number
Example
Java program to input an integer and print its absolute value.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int n,x;
Scanner sc=new Scanner(System.in);
System.out.print("Enter an integer number ");
n=sc.nextInt();
x=Math.abs(n);
System.out.println("Absolute value of " + n + " is " + x);
}
}
Output
Enter an integer number -18 Absolute value of -18 is 18
Here you can see that we have input -18 and pass the value to the abs() function. The abs() function returned the result in the variable x after removing the negative sign from the number. Thus the final output is 18.
min() Function
The min() function returns the smallest among two numbers. The number can be an integer, long, float or double value.
Syntax of min() function
int min(int a, int b); // Returns the smallest among two integer data type numbers
long min(long a, long b); // Returns the smallest among two long data type numbers
float min(float a, float b); // Returns the smallest among two float data type numbers
double min(double a, double b); // Returns the smallest among two double data type numbers
Example
Java program to input two integers and print the smallest value on the screen.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integer numbers");
a=sc.nextInt();
b=sc.nextInt();
c=Math.min(a,b);
System.out.println("Smallest number = " + c);
}
}
Output
Enter two integer numbers 54 96 Smallest number = 54
Here you can see that we have input 54 and 96 and pass the values to the min() function. The min() function returned the result in the variable c after finding the smallest among two numbers. Thus the final output is 54.
max() Function
The max() function returns the greater among two numbers. The number can be an integer, long, float or double value.
Syntax of max() function
int max(int a, int b); // Returns the greater among two integer data type numbers
long max(long a, long b); // Returns the greater among two long data type numbers
float max(float a, float b); // Returns the greater among two float data type numbers
double max(double a, double b); // Returns the greater among two double data type numbers
Example
Java program to input two integers and print the greater value on the screen.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integer numbers");
a=sc.nextInt();
b=sc.nextInt();
c=Math.max(a,b);
System.out.println("Greater number = " + c);
}
}
Output
Enter two integer numbers 54 96 Greater number = 96
Here you can see that we have input 54 and 96 and pass the values to the max() function. The max() function returned the result in the variable c after finding the greatest among two numbers. Thus the final output is 96.
sqrt() Function
The sqrt() function returns the square root of a positive number. Remember that square root of a negative can not be calculated.
Syntax of sqrt() function
double sqrt(double num);
A sqrt() function takes input in double data type and return the result in double data type. You can also pass an integer data type number to the sqrt() function but the number will implicitly convert into double data type as shown in the example below.
Example
Java program to input an integer and print its square root.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int n;
double x;
Scanner sc=new Scanner(System.in);
System.out.print("Enter an integer number ");
n=sc.nextInt();
x=Math.sqrt(n);
System.out.println("Square root of " + n + " is " + x);
}
}
Output
Enter an integer number 25 Square root of 25 is 5.0
Here you can see that we have input 25 and pass the value to the sqrt() function. The sqrt() function returned the result in the variable x after calculating the square root. Thus the final output is 5.0.
ceil() Function
The ceil() function returns the nearest integer number greater than the number passed as argument.
Syntax of ceil() function
double ceil(double num);
A ceil() function takes input in double data type and return the result in double data type. You can also pass a float data type number to the ceil() function but the number will implicitly convert into double data type as shown in the example below.
Example
Java program to input a floating point number and print its ceil value.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
float n;
double x;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a floating point (decimal) number ");
n=sc.nextFloat();
x=Math.ceil(n);
System.out.println("Ceil value of " + n + " is " + x);
}
}
Output
Enter a floating point (decimal) number 12.4 Ceil value of 12.4 is 13.0
Here you can see that we have input 12.4 and pass the value to the ceil() function. The ceil() function returned the result in the variable x after calculating the ceil value of variable n. Thus the final output is 13.0.
floor() Function
The floor() function returns the nearest integer number less than the number passed as argument.
Syntax of floor() function
double floor(double num);
A floor() function takes input in double data type and return the result in double data type. You can also pass a float data type number to the floor() function but the number will implicitly convert into double data type as shown in the example below.
Example
Java program to input a floating point number and print its floor value.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
float n;
double x;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a floating point (decimal) number ");
n=sc.nextFloat();
x=Math.floor(n);
System.out.println("Floor value of " + n + " is " + x);
}
}
Output
Enter a floating point (decimal) number 12.4 Floor value of 12.4 is 12.0
Here you can see that we have input 12.4 and pass the value to the floor() function. The floor() function returned the result in the variable x after calculating the floor value of variable n. Thus the final output is 12.0.
pow() Function
The pow() function is used to computes the power of a number.
Syntax of pow() function
double pow(double x, double y);
In mathematics the power is written as xy.
A pow() function takes two input (first as base value and second as power value) in double data type and return the result in double data type. You can also pass an integer or float data type numbers to the pow() function but the numbers will implicitly convert into double data type as shown in the example below.
Example
Java program to input two integer numbers and print the power.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int a,b;
double c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integer numbers");
a=sc.nextInt();
b=sc.nextInt();
c=Math.pow(a,b);
System.out.println("Power = " + c);
}
}
Output
Enter two integer numbers 5 2 Power = 25.0
Here you can see that we have input two integer numbers 5 and 2 and pass the values to the pow() function. The pow() function returned the result in the variable c after calculating the power. Thus the final output is 25.0.
All the above functions are most commonly used mathematical functions. Below is the list of other mathematical functions that you can use in your program as per requirement.
List of other useful mathematical functions
Function | Syntax | Description |
round | int round(float x) | The round() function returns the closest round up value of x. |
round | long round(double x) | The round() function returns the closest round up value of x. |
rint | double rint(double x) | The rint() function returns the double value that is closest in value to the argument and is equal to a mathematical integer. |
cos | double cos(double x) | The cos() function returns the cosine of x, where x is expressed in radians. The return value of cos() is in the range [-1, 1]. |
acos | double acos(double x) | The acos() function returns the arc cosine of x, which will be in the range [0, pi]. x should be between -1 and 1. Value of pi=3.14159265. |
cosh | double cosh(double x) | The function cosh() returns the hyperbolic cosine of x. |
sin | double sin(double x) | The function sin() returns the sine of x, where x is given in radians. The return value of sin() will be in the range [-1, 1]. |
asin | double asin(double x) | The asin() function returns the arc sine of x, which will be in the range [-pi/2, +pi/2]. x should be between -1 and 1. Value of pi=3.14159265. |
sinh | double sinh(double x) | The function sinh() returns the hyperbolic sine of x. |
tan | double tan(double x) | The tan() function returns the tangent of x, where x is given in radians. |
atan | double atan(double x) | The function atan() returns the arc tangent of x, which will be in the range [-pi/2, +pi/2]. Value of pi=3.14159265. |
atan2 | double atan2(double y, double x) | The atan2() function computes the arc tangent of y/x, using the signs of the arguments to compute the quadrant of the return value. |
tanh | double tanh(double x) | The function tanh() returns the hyperbolic tangent of x. |
exp | double exp(double x) | The exp() function returns e (2.7182818) raised to the xth power. |
log | double log(double x) | The function log() returns the natural (base e) logarithm of x. |
log10 | double log10(double x) | The log10() function returns the base 10 (or common) logarithm for x. |
Test Your Knowledge
Attempt the practical questions to check if the lesson is adequately clear to you.