'Find smallest number among five numbers
I need input 5 numbers and return the smallest. I try to return with Math.min but I can return just 3 arguments.
Scanner in = new Scanner(System.in);
System.out.print("Input the first number: ");
double x = in.nextDouble();
System.out.print("Input the Second number: ");
double y = in.nextDouble();
System.out.print("Input the third number: ");
double z = in.nextDouble();
System.out.print("Input the fourth number: ");
double a = in.nextDouble();
System.out.print("Input the fifth number: ");
double b = in.nextDouble();
System.out.print("The smallest value is " + smallest(x, y, z, a, b)+ );
}
public static double smallest(double x, double y, double z, double a, double c)
{
return Math.min(Math.min(x, y, z, a, b)) // thats failed
return Math.min(Math.min(x,y), z)) //thats works
}
Solution 1:[1]
Your first example uses b instead of c, and Math.min(double, double) compares two elements at a time. So you need to nest more calls. Something like,
return Math.min(Math.min(x, y), Math.min(Math.min(z, a), c));
Or, if using Java 8+, you could build a DoubleStream and take the minimum of that. Like,
return DoubleStream.of(x, y, z, a, c).min().orElse(-1);
Solution 2:[2]
You can do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double n, min = 0.0;
System.out.print("Enter a number: ");
n = in.nextDouble();
min = n;
for (int i = 1; i <= 4; i++) {
System.out.print("Enter a number: ");
n = in.nextDouble();
if (n < min) {
min = n;
}
}
System.out.println("Smallest number is: " + min);
}
}
A sample run:
Enter a number: 5.56
Enter a number: 3.56
Enter a number: 2000
Enter a number: 45.3
Enter a number: 90
Smallest number is: 3.56
[Update]
Given below is another simple way as suggested by @Arnaud Denoyelle:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double min = Double.MAX_VALUE;
for (int i = 1; i <= 5; i++) {
System.out.print("Enter a number: ");
min = Math.min(min, in.nextDouble());
}
System.out.println("The smallest number is: " + min);
}
}
A sample run:
Enter a number: 4.5
Enter a number: 1.2
Enter a number: 6.7
Enter a number: 1.1
Enter a number: 90
The smallest number is: 1.1
Solution 3:[3]
You'll find it easier if you use an array or a list in this kind of problems to make the same code work for any amount of elements. After all, you would find it quite difficult to keep doing it once you had 10, 100 or 1000 numbers, and more likely the number would be dynamic and static code would not work.
In Java canonical solutions would be either to use an array (Double[] or double[]) or one of the collections in the collection api of which java.util.List would be the most straight-forward. You need not code your min-function yourself either (except for practicing) because with arrays you can use java.util.Arrays.min and with collections java.util.Collections.min helper methods.
Here is a simple solution using lists, but for learning you might want to do the same with arrays yourself.
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class Numbers {
public static void main(String[] args) {
final int NUM = 5;
List<Double> list = new ArrayList<>();
try (Scanner in = new Scanner(System.in)) {
for (int i = 0; i < NUM; ++i) {
list.add(in.nextDouble());
}
}
Double smallest = Collections.min(list);
System.out.println("The smallest number is: " + smallest);
}
}
Not that your idea to use Math.min method is wrong: you could use it in a loop if you implemented your own version of min(List<Double>) (which you shouldn't except for learning). This is because min is an associative operation, which can be seen from the way you wrote it: min(x,min(y, z)). It's more that handling a multitude of elements is best done with an array-like data structure.
Solution 4:[4]
Using Collections API you can do:
return Collections.min(Arrays.asList(x,y,z,a,b));
Solution 5:[5]
Code to find Smallest Number using array (Easy and Simple)
int temp;
int [] a = {10,5,30,4,9,75,2,4,13,55,44,0,14,23,14}; // Assumed Numbers
temp = a[0];
for(int i = 1;i< a.length;i++) {
if(temp < a[i]) {
}else {
temp =a[i];
}
}
System.out.println("Smallest Value is " + temp);
Solution 6:[6]
The min function only takes 2 values and evaluates them against each other. Try to rewrite the smallest function to take the 5 numbers as a list. This makes the function more dynamic too. If you are not restricted to use the min function you can then sort the list and return the smallest element of the list.
If you strictly want to use Math.min you need to to some further nesting like:
Math.min((Math.min(Math.min(x, y), Math.min(z,a))), b)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elliott Frisch |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | KKR |
| Solution 5 | Sourabh Kumar |
| Solution 6 |
