Sem 5‎ > ‎JAVA LAB‎ > ‎

P1: WAP to find greatest of three numbers.

posted Aug 19, 2012, 10:02 AM by Neil Mathew   [ updated Aug 19, 2012, 10:09 AM ]

INPUT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class BiggerOf
{
        public static void main( String thenumbers[] )
        {
        
        //To ensure only three inputs are added:
        //Exception Handling:
        if( thenumbers.length != 3 )
        {
        System.out.println("Only Three Numbers at a time!");
        return;
        }
        
        int number1 = Integer.parseInt(thenumbers[0]);
        int number2 = Integer.parseInt(thenumbers[1]);
        
        int number3 = Integer.valueOf(thenumbers[2]);
        
        if( number1 > number2 && number1 > number3)             
        System.out.println("Number "+number1+
                                        " is the bigger of the three!");
        
        else if( number2 > number1 && number2 > number3)                
        System.out.println("Number "+number2+
                                        " is the bigger of the three!");
        
        else    
        System.out.println("Number "+number3+
                                        " is the bigger of the three!");
        
        
        }
}
 


OUTPUT:


C:\Java 1.6\jdk1.6.0_30\bin>javac src/BiggerOf.java

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf

Only Three Numbers at a time!

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf 16 2 34

Number 34 is the bigger of the three!

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf 13 24 7

Number 24 is the bigger of the three!

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf 19 5 3

Number 19 is the bigger of the three!

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf 1 0

Only Three Numbers at a time!

 



Comments