forked from ironhack-labs/lab-java-loops-and-version-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1lab2.java
More file actions
41 lines (36 loc) · 1.32 KB
/
Task1lab2.java
File metadata and controls
41 lines (36 loc) · 1.32 KB
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
40
public class Task1lab2 {
// encontrar el número más pequeño en un array
public static int encontrarMinimo(int[] numeros) {
int minimo = numeros[0]; // Inicializamos con el primer valor
for (int num : numeros) {
if (num < minimo) {
minimo = num;
}
}
return minimo;
}
// encontrar el número más grande en un array
public static int encontrarMaximo(int[] numeros) {
int maximo = numeros[0];
for (int num : numeros) {
if (num > maximo) {
maximo = num;
}
}
return maximo;
}
// diferencia entre el mayor y el menor valor
public static int diferenciaMaxMin(int[] numeros) {
if (numeros.length < 1) {
throw new IllegalArgumentException("El array debe tener mínimo 1 elemento.");
}
return encontrarMaximo(numeros) - encontrarMinimo(numeros);
}
// main
public static void main(String[] args) {
int[] numeros = {85, 4, 63, 21, 54};
System.out.println("El número más pequeño es: " + encontrarMinimo(numeros));
System.out.println("El número más grande es: " + encontrarMaximo(numeros));
System.out.println("La diferencia entre el máximo y el mínimo es: " + diferenciaMaxMin(numeros));
}
}