Java Program to accept three numbers and print the second lowest number
Explanation: First of all, we will take input using Buffer Reader. We take 3 integer values as input and store them in a, b and c respectively. For a number to be second greatest among three numbers, the number must be greater than the one number and less than the another.
Keeping this in mind, we have checked if a is greater than b and is less than c or if a is greater than c and is less than b. If the condition is satisfied then a is the second largest number. Same goes for the other condition too. In the last we haven't written any condition as it is already understood that if a and b are not the second largest integer, then c is the second largest.
Example:
Sample Input: 46, 78, 33 Sample Output: 46
Code:
Using BufferedReader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.io.*; class SecondLowest { public static void main() throws Exception() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //taking input System.out.println("Enter the first number:"); int a = Integer.parseInt(br.readLine()); System.out.println("Enter the second number:"); int b = Integer.parseInt(br.readLine()); System.out.println("Enter the third number:"); int c = Integer.parseInt(br.readLine()); //checking for the second largest number if (a > b && a < c || a > c && a < b) System.out.println(a); else if (b > a && b < c || b > c && b < a) System.out.println(b); else System.out.println(c); } } |
Using Scanner Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.*; class SecondLowest { public static void main() { Scanner sc = new Scanner(System.in); //taking input System.out.println("Enter the first number:"); int a = sc.nextInt(); System.out.println("Enter the second number:"); int b = sc.nextInt(); System.out.println("Enter the third number:"); int c = sc.nextInt(); //checking for the second largest number if(a>b && a<c || a>c && a<b) System.out.println(a); else if(b>a && b<c || b>c && b<a) System.out.println(b); else System.out.println(c); } } |
S.No | Variable Name | Data Type | Purpose |
1. | a | int | To Store the first number. |
2. | b | int | To Store the second number. |
3. | c | int | To Store the third number. |
Join Us
For those who prefer reading on mobile, we have Telegram channel and WhatsApp group that will allow you to receive updates, announcements, and links to our stories straight to your mobile device..
Also check:
https://www.theschoolprogrammer.com/2020/04/isc-2020-computer-science-practical-paper-solved.html
Found this useful? Share with your friends. Share
thank u, it was of great help
ReplyDelete