Following is the implementation of the find maximum of three integers in Java.
package basics; public class ThreeComparator { public static int max3( int a, int b, int c ) { if( a>b ) { if( a > c ) return a; else return c; } else if( b>c ) { return b; } else { return c; } } public static void main(String[] args) { int a = 4; int b = 3; int c = 5; System.out.println("Maximum of "+a+" , "+b+" and "+c+" is = "+max3( a,b,c )); System.out.println("Maximum of "+a+" , "+c+" and "+b+" is = "+max3( a,c,b )); System.out.println("Maximum of "+b+" , "+a+" and "+c+" is = "+max3( b,a,c )); System.out.println("Maximum of "+b+" , "+c+" and "+a+" is = "+max3( b,c,a )); System.out.println("Maximum of "+c+" , "+a+" and "+b+" is = "+max3( c,a,b )); System.out.println("Maximum of "+c+" , "+b+" and "+a+" is = "+max3( c,b,a )); System.out.println("Maximum of 4 , 5 and 4 is = "+max3( 4,5,4 )); System.out.println("Maximum of 41 , 41 and 41 is = "+max3( 41,41,41 )); System.out.println("Maximum of 11 , 21 and 31 is = "+max3( 11,21,31 )); } }
Create a ThreeComparator.java file in your workspace.
When the main method inside the ThreeComparator class executed it is going to print :
Maximum of 4 , 3 and 5 is = 5
Maximum of 4 , 5 and 3 is = 5
Maximum of 3 , 4 and 5 is = 5
Maximum of 3 , 5 and 4 is = 5
Maximum of 5 , 4 and 3 is = 5
Maximum of 5 , 3 and 4 is = 5
Maximum of 4 , 5 and 4 is = 5
Maximum of 41 , 41 and 41 is = 41
Maximum of 11 , 21 and 31 is = 31
No comments:
Post a Comment