In this blog, we’ll explore various approaches to finding the smallest of three numbers in Java, each catering to different scenarios and use cases.
When working with numbers in Java, it’s often necessary to find the smallest among them. Whether you’re building a sorting algorithm, processing data, or handling user inputs, knowing how to identify the smallest number efficiently is essential.
Find Smalllest of Three Numbers in Java Using Ternary Operator
//Smallest of Three Numbers in Java import java.util.Scanner; public class SmallestNumEx { public static void main(String[] args) { int x, y, z, smallest, temp; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user System.out.println("Enter the first number:"); x = sc.nextInt(); System.out.println("Enter the second number:"); y = sc.nextInt(); System.out.println("Enter the third number:"); z = sc.nextInt(); //comparing a and b and storing the smallest number in a temp variable temp=x<y?x:y; //comparing the temp variable with c and storing the result in the variable names smallest smallest=z<temp?z:temp; //prints the smallest number System.out.println("The smallest number is: "+smallest); } }
Explanation of the code:
Java code is designed to find the smallest among the three numbers entered by the user. Below is an explanation of how the code works:
1. The program starts by declaring integer variables `x`, `y`, and `z` to store the three input numbers and an additional variable `smallest` to store the result (the smallest number).
2. A `Scanner` object named `sc` is created to read user input from the console.
3. The user is prompted to enter three numbers, which are then read using `sc.nextInt()` and stored in variables `x`, `y`, and `z`.
4. The code uses the ternary operator to compare `x` and `y`, and the smaller of the two is stored in the temporary variable `temp`.
5. The ternary operator is again used to compare `temp` and `z`, and the smallest among the three numbers is stored in the variable `smallest`.
6. Finally, the program prints the result using `System.out.println()`, displaying the smallest number among the three entered by the user.
This code efficiently finds and displays the smallest number among the three input values, making it a simple and effective way to solve the problem.
Output:
Enter the first number:
87
Enter the second number:
54
Enter the third number:
36
The smallest number is: 36
Find the smallest of three numbers using a single statement
//Smallest of Three Numbers in Java import java.util.Scanner; public class SmallestNumEx2 { public static void main(String[] args) { int x, y, z, smallest; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user System.out.println("Enter the first number:"); x = sc.nextInt(); System.out.println("Enter the second number:"); y = sc.nextInt(); System.out.println("Enter the third number:"); z = sc.nextInt(); smallest = z < (x < y ? x : y) ? z : ((x < y) ? x : y); System.out.println("The smallest number is: "+smallest); } }
Explanation of the code:
Java code is designed to find the smallest among the three numbers entered by the user. Here’s an explanation of how the code works:
1. The program declares integer variables `x`, `y`, and `z` to store the three input numbers and an additional variable `smallest` to store the result (the smallest number).
2. A `Scanner` object named `sc` is created to read user input from the console.
3. The user is prompted to enter three numbers, which are then read using `sc.nextInt()` and stored in variables `x`, `y`, and `z`.
4. The code uses nested ternary operators to compare the three numbers and assigns the smallest value to the variable `smallest`. The expression `(x < y ? x : y)` finds the smaller of `x` and `y`, and then the outer ternary operator compares the result with `z`, assigning the smallest value to `smallest`.
5. Finally, the program prints the result using `System.out.println()`, displaying the smallest number among the three entered by the user.
This code efficiently finds and displays the smallest number among the three input values, providing a concise and effective solution to the problem.
Output:
Enter the first number:
45
Enter the second number:
65
Enter the third number:
0
The smallest number is: 0
Find Smalllest of Three Numbers in Java Using if else..if
//Smallest of Three Numbers in Java import java.util.Scanner; public class SmallestNumEx3 { public static void main(String[] args) { //initializing numbers to compare int a, b, c, smallest; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user System.out.println("Enter the first number:"); a = sc.nextInt(); System.out.println("Enter the second number:"); b = sc.nextInt(); System.out.println("Enter the third number:"); c = sc.nextInt(); //comparing numbers, a with b and a with c //if both conditions are true, prints a if(a<=b && a<=c) System.out.println(a+" is the smallest number"); //comparing b with a and b with c //if both conditions are true, prints b else if (b<=a && b<=c) System.out.println(b+" is the smallest number"); else //prints c if the above conditions are false System.out.println(c+" is the smallest number"); } }
Explanation of the code:
The provided Java code determines the smallest of three numbers (a, b, and c) using conditional statements. It begins by initializing variables to hold the three input numbers and an additional variable ‘smallest’ to store the result. The program prompts the user to enter three numbers using the Scanner class.
The code then compares the numbers using conditional statements:
1. It first checks if ‘a’ is the smallest by comparing ‘a’ with both ‘b’ and ‘c’. If ‘a’ is smaller or equal to both, it prints that ‘a’ is the smallest.
2. If the first condition is false, it checks if ‘b’ is the smallest by comparing ‘b’ with both ‘a’ and ‘c’. If ‘b’ is smaller or equal to both, it prints that ‘b’ is the smallest.
3. If both the above conditions are false, it means ‘c’ is the smallest among the three numbers, and it prints that ‘c’ is the smallest.
The code efficiently identifies and displays the smallest number among the inputs provided by the user.
Learn How to Generate Random Numbers in Python, Now!
Output:
Enter the first number:
56
Enter the second number:
85
Enter the third number:
42
42 is the smallest number
Find Smalllest of Three Numbers in Java Using nested-if Statement
//Smallest of Three Numbers in Java import java.util.Scanner; public class SmallestNumEx4 { public static void main(String[] args) { int a, b, c; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user System.out.println("Enter the first number:"); a = sc.nextInt(); System.out.println("Enter the second number:"); b = sc.nextInt(); System.out.println("Enter the third number:"); c = sc.nextInt(); if(a<=b) { if(a<=c) //prints a, if the above two conditions are true System.out.println("The smallest number is: "+a); else //prints c, if the condition defined in outer if is true and the condition defined in inner if is false System.out.println("The smallest number is: "+c); } else { if(b<=c) //prints b, if the condition defined in outer if is false and the condition defined in inner if is true System.out.println("The smallest number is: "+b); else //prints c, if the condition defined in both inner and outer loop is false System.out.println("The smallest number is: "+c); } } }
Explanation of the code:
Java code determines the smallest of three numbers (a, b, and c) using nested conditional statements. It begins by initializing variables to hold the three input numbers. The program prompts the user to enter three numbers using the Scanner class.
The code then compares the numbers using nested if-else statements:
1. It first checks if ‘a’ is smaller than or equal to ‘b’. If true, it proceeds to another if-else statement to check if ‘a’ is smaller than or equal to ‘c’. If both conditions are true, it prints that ‘a’ is the smallest.
2. If the first condition is false, it means ‘b’ is greater than ‘a’. It then checks if ‘b’ is smaller than or equal to ‘c’. If true, it prints that ‘b’ is the smallest.
3. If both the above conditions are false, it means ‘c’ is the smallest among the three numbers, and it prints that ‘c’ is the smallest.
The code efficiently identifies and displays the smallest number among the inputs provided by the user using nested conditional statements.
Output:
Enter the first number:
45
Enter the second number:
87
Enter the third number:
53
The smallest number is: 45
Understand the Concept of GCD of Two Numbers in Java, Here!
Find the smallest of three integers (n1, n2, and n3) using if-else conditional statements
import java.util.Scanner; public class SmallestNumEx5 { public static void main(String args[]) { int n1, n2, n3; System.out.println("Enter three integers: "); Scanner in = new Scanner(System.in); n1=in.nextInt(); n2=in.nextInt(); n3=in.nextInt(); if (n1 < n2 && n1 < n3) System.out.println("The smallest number is: "+n1); else if (n2 < n1 && n2 < n3) System.out.println("The smallest number is: "+n2); else if (n3 < n1 && n3 < 2) System.out.println("The smallest number is: "+n3); else System.out.println("The numbers are same."); } }
Also, learn about Largest of Three Numbers in Java, Now!
Explanation of the code:
The provided Java code determines the smallest of three integers (n1, n2, and n3) using if-else conditional statements. It starts by prompting the user to enter three integers using the Scanner class.
The code then compares the three numbers using if-else statements:
1. It checks if ‘n1’ is smaller than both ‘n2’ and ‘n3’. If true, it prints that ‘n1’ is the smallest.
2. If the first condition is false, it checks if ‘n2’ is smaller than both ‘n1’ and ‘n3’. If true, it prints that ‘n2’ is the smallest.
3. If both the above conditions are false, it checks if ‘n3’ is smaller than both ‘n1’ and ‘n2’. If true, it prints that ‘n3’ is the smallest.
4. If none of the above conditions are true, it means at least two of the three numbers are equal. It then prints “The numbers are same.”
The code efficiently identifies and displays the smallest number among the three input integers using if-else conditional statements.
Output:
Enter three integers:
5
45
45
The smallest number is: 5
In conclusion, the ability to find the smallest of three numbers is just a small step in your Java journey. We hope that this article has helped you learn more about the “Smallest of three in Java” and other important information. Stay tuned for more updates from Java programming blogs. You can also visit our Newtum website for more information on various programs such as PHP, C Programming for kids, and more. Happy coding!
FAQ’s on Finding Smallest of Three Numbers in Java
Using nested ternary operators is a concise and efficient method.
It is essential for tasks like sorting algorithms, data processing, and handling user inputs.
It compares two values and assigns the smaller one, then compares the result with the third value.
Yes, by using nested ternary operators, you can achieve this in a single statement.
It sequentially compares the numbers and determines the smallest based on the conditions.