-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursividad2.java
More file actions
26 lines (22 loc) · 802 Bytes
/
Recursividad2.java
File metadata and controls
26 lines (22 loc) · 802 Bytes
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
import java.util.Scanner;
public class Recursividad2 {
//Buscar un elemento de un array de forma recursiva.
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] array={3, 5, 8, 90, 54, -3};
System.out.println("Número que deseas buscar: ");
int buscado = sc.nextInt();
System.out.println("El numero buscado se encuentra en la posición: " + buscar(array, buscado, 0));
}
public static int buscar(int[] array, int buscado, int index){
if(index == array.length || array[index] == buscado){
if(index == array.length){
return -1;
}else{
return index;
}
}else{
return buscar(array, buscado, index + 1);
}
}
}