## Math Class Methods in java with examples

Math class has different methods for finding power, absolute value, min, max and rounding number etc

Syntax:-

Math.methods()

Here methods is replaced by different math class methods word

1. absolute value using java Math Class

Syntax:-

Math.abs(variable)

Here variable can be double,float,int and long datatype.

abs method is used to find absolute value of variable.

Math.abs(-2.3) = 2.3

2. minimum value using java Math Class

Syntax:-

Math.min(variable1,variable2)

Here variable can be double,float,int and long datatype

min method is used to find minimum value between variable1 and variable2.

Math.min(2,8) = 2

3. maximum value using java Math Class

Syntax:-

Math.max(variable1,variable2)

Here variable can be double,float,int and long datatype

max method is used to find maximum value between variable1 and variable2.

Math.max(2,8) = 8

4.Rounding number using java Math Class

Syntax:-

Math.ceil(variable)

Math.floor(variable)

Here variable is double datatype

ceil method is used round up the variable and floor method is used round down the variable.

Math.ceil(8.4) = 9

Math.floor(8.4) = 8

5. power of the number using java Math Class

Syntax:-

Math.pow(variable1,variable2)

Here variable is double datatype

here variable1 is base value and variable2 is power value.

Math.pow(2,3) = 8

6. square root of the number using java Math Class

Syntax:-

Math.sqrt(variable)

Here variable is double datatype

sqrt method is used for finding square root of a number.

Math.sqrt(9) = 3

7. cube root of the number using java Math Class

Syntax:-

Math.cbrt(variable)

Here variable is double datatype

cbrt method is used for finding cube root of a number.

Math.cbrt(27) = 3

8. copy sign of the number using java Math Class

Syntax:-

Math.copySign(variable1, variable2)

Here variable is float and double datatype

copySign method is used to copy sign of variable2 and assign that sign to variable1.

Math.copySign(2,-8.3) = -2.0

Math.copySign(2,8.3) = 2.0

9. Euler's number e to power of a number using java Math Class

Syntax:-

Math.exp(variable)

Here variable is double datatype

Euler's number e raised to the power of a variable.

Math.exp(2) = 7.38905609893065

10. Euler's number e to power of a number minus 1 using java Math Class

Syntax:-

Math.expm1(variable)

Here variable is double datatype

Euler's number e raised to the power of a variable minus 1 ie e^(variable)-1

Math.expm1(2) = 6.38905609893065

Example:-

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
double num1,num2,num;
System.out.println("Enter a number");
num1=data.nextInt();
System.out.println("Enter a number");
num2=data.nextInt();

num=Math.abs(-2.5);
System.out.println("absolute:"+num);

num=Math.min(num1,num2);
System.out.println("min:"+num);

num=Math.max(num1,num2);
System.out.println("max:"+num);

num=Math.ceil(8.4);
System.out.println("ceil:"+num);

num=Math.floor(8.4);
System.out.println("floor:"+num);

num=Math.pow(num1, num2);
System.out.println("power:"+num);

num=Math.sqrt(num1);
System.out.println("sqrt:"+num);

num=Math.cbrt(num2);
System.out.println("cbrt:"+num);

num=Math.copySign(num1,-8.2);
System.out.println("copysign:"+num);

num=Math.exp(2);
System.out.println("exp:"+num);

num=Math.expm1(2);
System.out.println("expm1:"+num);

}
}

Output:-

Enter a number
8
Enter a number
27
absolute:2.5
min:8.0
max:27.0
ceil:9.0
floor:8.0
power:2.4178516392292583E24
sqrt:2.8284271247461903
cbrt:3.0
copysign:-8.0
exp:7.38905609893065
expm1:6.38905609893065

Previous Code:-
Nesting loop in java

List Code:-
Java codes

Next Code:-
Logarithm function in java

Java Programs:-
List of Java Programs

### One Comment

1. what about the equivalent using math class method ?