Wednesday 1 June 2022

Java math min(): Find the minimum of two numbers

‘java.lang.Math’ class provides min method to get the smaller of two numbers. ‘min’ method is available in below overloaded forms.

 

public static double min(double a, double b)

public static int min(int a, int b)

public static long min(long a, long b)

public static float min(float a, float b)

‘min’ method return the minimum of a and b.

 

Example

Math.min(10, 20)

 


MathMinDemo.java

package com.sample.app;

public class MathMinDemo {
	
	public static void main(String[] args) {
		int a = 10;
		int b = 11;
		
		long c = 12l;
		long d = 13l;
		
		float e = 3.23f;
		float f = 3.24f;
		
		double g = 4.23;
		double h = 4.24;
		
		System.out.println("Minimum of " + a + " and " + b + " is : " + Math.min(a, b));
		System.out.println("Minimum of " + c + " and " + d + " is : " + Math.min(c, d));
		System.out.println("Minimum of " + e + " and " + f + " is : " + Math.min(e, f));
		System.out.println("Minimum of " + g + " and " + h + " is : " + Math.min(g, h));
	}

}

 

Output

Minimum of 10 and 11 is : 10
Minimum of 12 and 13 is : 12
Minimum of 3.23 and 3.24 is : 3.23
Minimum of 4.23 and 4.24 is : 4.23

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment