In this java tutorial. I will show you how to find the second number amoung three numbers with two alternative way.
Accept three numbers from the user and display the second largest number in java.
Way 1:
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 |
import java.util.*; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); //taking input System.out.print("Enter the first number:"); int num1 = sc.nextInt(); System.out.print("Enter the second number:"); int num2 = sc.nextInt(); System.out.print("Enter the third number:"); int num3 =sc.nextInt(); //checking for the second largest number if (num1 > num2 && num1 < num3 || num1 > num3 && num1 < num2) System.out.println("The second largest number is "+num1); else if (num2 > num1 && num2 < num3 || num2 > num3 && num2 < num1) System.out.println("The second largest number is "+num2); else System.out.println("The second largest number is "+num3); } } |
This code prompts the user to enter three integers and then finds the second largest number among them. Here is a brief explanation of how it works:
- The Scanner class is used to read input from the user.
- The program prompts the user to enter three integers, which are stored in variables num1, num2, and num3.
- The program checks which of the three numbers is the second largest. It does this by using a series of if-else statements that check various conditions.
- If one of the conditions is true, the program prints out the second largest number. If none of the conditions are true, the program prints out the third number, which is the smallest of the three.
- The program then ends.
Output:
1 2 3 4 5 6 |
Enter the first number:10 Enter the second number:20 Enter the third number:30 The second largest number is 20 |
Way 2:
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 35 36 37 38 39 |
import java.util.*; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); //taking input System.out.print("Enter the first number:"); int num1 = sc.nextInt(); System.out.print("Enter the second number:"); int num2 = sc.nextInt(); System.out.print("Enter the third number:"); int num3 =sc.nextInt(); int largest=0,smallest=0; //largest if(num1 > num2 && num1 > num3) largest = num1; else if (num2 > num3) largest = num2; else largest = num3; //smallest if(num1 < num2 && num1 < num3) smallest = num1; else if (num2 < num3) smallest = num2; else smallest = num3; int second = num1 + num2 + num3 -(largest+smallest); System.out.println("The second largest number is "+second); } } |
This code prompts the user to enter three integers and then finds the second largest number among them. It does this by first finding the largest and smallest numbers, and then subtracting those two numbers from the sum of all three numbers. The result is the second largest number. Here is a brief explanation of how it works:
- The Scanner class is used to read input from the user.
- The program prompts the user to enter three integers, which are stored in variables num1, num2, and num3.
- The program uses a series of if-else statements to find the largest and smallest numbers. It does this by comparing the values of num1, num2, and num3 and setting the variables largest and smallest accordingly.
- The program then calculates the second largest number by subtracting the largest and smallest numbers from the sum of all three numbers.
- The program prints out the second largest number and then ends.
Output:
1 2 3 4 5 6 |
Enter the first number:10 Enter the second number:20 Enter the third number:30 The second largest number is 20 |
The program(the first way) does not work with values 5, 5, and 6. This is because 5 is not greater than 5, then the program moves to ‘else’ and prints 6 which is the wrong result. we can use relation operators(Specifically ‘ = ‘ instead is only ‘ ‘ ) to correct this and also shorten the program.
we should use relational operators =
we should use relational operators =
we should use relational operators =