Java Math

We will cover Java math with examples in today’s lesson to better meet learners’ needs.

You can perform mathematical operations on numbers using the Java Math class

The Math class consists of numerous methods to perform math operations, such as

  • min()
  • max()
  • avg()
  • sin()
  • cos()
  • tan()
  • round()
  • ceiling()
  • floor()
  • abs()

 

Math.max(mrx,ample)

Math.max(mrx,ample) can be used to find the highest value among two numbers:

Example: 

public class Main { public static void main(String[] args) { int mrx=10; int ample=20; System.out.println("The highest value is: "+Math.max(mrx, ample)); } }

Example: 

public class Main { public static void main(String[] args) { int mrx_age=15; int ample_age=21;System.out.println("The highest value is: "+Math.max(mrx_age, ample_age)); } }


Math.min(mrx,ample)

Math.max(mrx,ample) can be used to find the lowest value of mrx and ample:

Example: 

public class Main { public static void main(String[] args) { int mrx=10; int ample=20; System.out.println("The minimum value among both is: "+Math.min(mrx, ample)); } }

Example: 

public class Main { public static void main(String[] args) { int mrx_num=50; int ample_num=70;System.out.println("The minimum value among both is: "+Math.min(mrx_num,ample_num )); } }

Math.sqrt(mrx)

Math.sqrt(mrx) returns the square root of mrx:

Example: 

public class Main { public static void main(String[] args) { int mrx=81; System.out.println("The Square root of "+mrx+" is : "+Math.sqrt(mrx)); } }

We can also find the root of a number in decimals:

Example: 

public class Main { public static void main(String[] args) { int ample=141; System.out.println("The Square root of "+ample+" is : "+(double)Math.sqrt(ample)); } }

Math.abs(mrx)

Math.abs(mrx) returns the absolute (positive) value of mrx:

Example: 

public class Main { public static void main(String[] args) { int mrx=-612; System.out.println("The positive value of "+mrx+" using Math.abs is: "+Math.abs(mrx)); } }

Example: 

public class Main { public static void main(String[] args) { float ample=-15.24f; System.out.println("The positive value of "+ample+" using Math.abs is: "+Math.abs(ample)); } }

Java Math – Random Numbers

The Math.random() function returns a random number between 0.0 (inclusive) and 1.0 (exclusive):

Example: 

public class Main { public static void main(String[] args) { System.out.println("Generating any number by the command: "+Math.random()); } }

Example: 

public class Main { public static void main(String[] args) { System.out.println("Generating any number Randomly: "+Math.random()); } }

Here’s a formula you can use if you want more control over the random number, such as a number between 0 and 200:

Example: 

public class Main { public static void main(String[] args) { int random_Num_Range = (int)(Math.random() * 201); // Any random number between 0 to 200 will be printed System.out.println(random_Num_Range); } }

Similarly to get a random number between 0 to 30 follow the example below:

Example: 

public class Main { public static void main(String[] args) { int random_Num_Range = (int)(Math.random() * 31); // Any random number between 0 to 30 will be printed System.out.println(random_Num_Range); } }

Complete Math Reference

There are various methods in the Java-Math class for performing basic numerical calculations, including logarithms, cube roots, and trigonometric functions. You can find a complete reference to Math methods in our Java Math Methods Reference.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *