Way 1 : find smallest number in java : Simple If else.
To find smallest number in java, you can use the following approach:
- Declare three variables to hold the three numbers.
- Compare the first number with the second number using an
if
statement. If the first number is smaller, assign it to a variable calledsmallest
. - Compare the
smallest
variable with the third number using anotherif
statement. If the third number is smaller, update thesmallest
variable to hold its value. - At this point, the
smallest
variable will contain the smallest number among the three.
Here’s an example code snippet that demonstrates this approach:
public class Main {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
int num3 = 7;
int smallest;
if (num1 < num2) {
smallest = num1;
} else {
smallest = num2;
}
if (num3 < smallest) {
smallest = num3;
}
System.out.println("The smallest number is: " + smallest);
}
}
In this example, the values 10
, 5
, and 7
are assigned to num1
, num2
, and num3
variables, respectively. The program compares these numbers and assigns the smallest value to the smallest
variable. Finally, it prints out the result, which in this case would be 5
.
Way 2 : find smallest number in java: by utilizing the Math.min()
method
Another way to find smallest number in java is by utilizing the Math.min()
method. The Math.min()
method returns the minimum of two given numbers.
public class SmallestNumberFinder {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
int num3 = 7;
int smallest = Math.min(num1, Math.min(num2, num3));
System.out.println("The smallest number is: " + smallest);
}
}
In this example, the Math.min()
method is used to find the minimum among num1
, num2
, and num3
. The result is assigned to the smallest
variable. Finally, it prints out the smallest number, which in this case would be 5
.
Using Math.min()
simplifies the code by avoiding multiple if
statements for comparisons and reducing the need for intermediate variables.
Way 3 : find Smallest number among three in java: by using a loop to iterate over the numbers.
Another way to find the smallest number among three numbers in Java is by using a loop to iterate over the numbers and keep track of the smallest value.
public class SmallestNumberFinder {
public static void main(String[] args) {
int[] numbers = {10, 5, 7};
int smallest = numbers[0]; // Assume the first number is the smallest
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i]; // Update the smallest if a smaller number is found
}
}
System.out.println("The smallest number is: " + smallest);
}
}
In this example, an array numbers
is used to store the three numbers. The smallest
variable is initially set to the first element of the array. Then, a for
loop is used to iterate over the remaining numbers. If a smaller number is found, it is assigned to the smallest
variable. Finally, it prints out the smallest number, which in this case would be 5
.
Using a loop provides flexibility if you have a dynamic set of numbers or if you want to find the smallest number among a larger collection of values.
Way 4 : find Smallest number among three in java: by using the ternary operator (? :
).
Another way to find the smallest number among three numbers in Java is by using the ternary operator (? :
).
public class SmallestNumberFinder {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
int num3 = 7;
int smallest = (num1 < num2) ? ((num1 < num3) ? num1 : num3) : ((num2 < num3) ? num2 : num3);
System.out.println("The smallest number is: " + smallest);
}
}
In this example, the ternary operator is used to compare the numbers in a nested manner. It first checks if num1
is smaller than num2
. If true, it further checks if num1
is smaller than num3
. If that condition is also true, num1
is assigned to the smallest
variable. If num1
is not smaller than num3
, num3
is assigned to the smallest
variable. If num1
is not smaller than num2
, it checks if num2
is smaller than num3
. If true, num2
is assigned to the smallest
variable. Otherwise, num3
is assigned. Finally, it prints out the smallest number, which in this case would be 5
.
Using the ternary operator allows for concise and inline comparisons, reducing the need for multiple if
statements or loops.
You can run above all code here just copy and paste.
I am so happy to read this. This is the type of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this greatest doc.