Java Program to accept three numbers and find Largest and Second largest numbs.

Java Program to identify largest and second largest number
  • Using if else
  • large , sLarge integer variables
  • 'Scanner' is a class in java.util used to read input in java programs , sc is the object of Scanner



*Program to accept 3 numbers and find 
 and second largest */
import java.util.Scanner;

public class largest {
	public static void main(String args[]) {
		int sLarge,large;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter First number :");
		int num1 = sc.nextInt();
		System.out.print("Enter Second number :");
		int num2 = sc.nextInt();
		System.out.print("Enter Third number :");
		int num3 = sc.nextInt();
		if (num1 >num2 && num1>num3) {
			 large = num1;
					if(num2>num3)
					    sLarge =num2;
					else
						sLarge =num3;
		}
		else if(num2>num3 && num3>num1) {
			large = num2;
			if(num1>num3)
				sLarge =num1;
			else
				sLarge =num3;
					
		}else {
			large=num3;
			if(num2>num1)
				sLarge=num2;
			else
				sLarge=num1;
		}
		
		System.out.println("Largest number is :"+large);
		System.out.println("Second largest number is :"+sLarge);
			
		
		
		
		
	}

}

Output

Enter First number :6
Enter Second number :1
Enter Third number :7
Largest number is :7
Second largest number is :6

Post a Comment

0 Comments