How to find Min and Max between two numbers?
--- we can use if statement and compare both the numbers and make decisions base on it.
for selecting Minimum
int a, b;
int min;
if(a >= b)
min = b;
for selecting Maximum
if(a >= b)
max = a;
If you are a regular developer or coder then it is fine to use this method but if you are a programmer who participates in different competitions. Implementation time performs well part to win or to perform well in the competition.
Java provides min() and max() methods to save time.
Both methods are under Math class.
Implementation
int a,b;
int min;
min = Math.min(a,b) ;
max = Math.max(a,b);
Using Ternary Operator
min = (a > b) ? b : a;
max = (a > b) ? a : b;
max = (a > b) ? a : b;
March 09, 2020
Tags :
Algorithm
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments
Please comment here...