Skip to content

Instantly share code, notes, and snippets.

@davidmanueldev
Created May 29, 2024 13:04
Show Gist options
  • Select an option

  • Save davidmanueldev/b4a43ed765a107b8f0d784f226f20d9d to your computer and use it in GitHub Desktop.

Select an option

Save davidmanueldev/b4a43ed765a107b8f0d784f226f20d9d to your computer and use it in GitHub Desktop.
Ejercicios con listas dobles
// Ejercicios con listas dobles
// Ejercicio 2: Obtener la lista A con elementos impares
// Ejemplo: L(6,9,2,7,12) => A(9,7)
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Ingrese el tamaño de la lista: ");
n = sc.nextInt();
int[] lista = new int[n];
int i = 0;
while (i < n) {
System.out.println("Ingrese el elemento " + (i + 1) + ": ");
lista[i] = sc.nextInt();
i++;
}
System.out.println("La lista A con elementos impares es: ");
int[] listaA = listaImpares(lista);
i = 0;
while (i < listaA.length) {
System.out.println(listaA[i]);
i++;
}
}
public static int[] listaImpares(int[] lista) {
int i = 0;
int j = 0;
while (i < lista.length) {
if (lista[i] % 2 != 0) {
j++;
}
i++;
}
int[] listaA = new int[j];
i = 0;
j = 0;
while (i < lista.length) {
if (lista[i] % 2 != 0) {
listaA[j] = lista[i];
j++;
}
i++;
}
return listaA;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment