Está en la página 1de 8

ARREGLOS

1. Realice un programa que rellene con una estructura cíclica, un array con los 100 primeros
números enteros y los muestre en pantalla.

public static void main(String[] args) {


// TODO code application logic here
int [] numeros = new int [100];
for (int i = 0; i < numeros.length; i++)
{

numeros[i]= i+1;
}
System.out.println("Contenido del arreglo");
for (int i = 0; i < numeros.length; i++)
{
System.out.printf("%d \n", numeros[i]);
}

}
2. Realice un programa que rellene un array con los números pares comprendidos entre 1 y 100
y los muestre en pantalla.

public static void main(String[] args) {


// TODO code application logic here
int [] numerosPares = new int [100];
for (int i = 1; i < numerosPares.length; i++)
{
numerosPares[i]=i;
if (numerosPares[i] % 2 == 0)
{
System.out.println(numerosPares[i]);
}
}
}
3. Crear un arreglo y cargar con 100 elementos con números aleatorios de entre 1-100 y muestre
los muestre el arreglo original y los múltiplos de 5.
public static void main(String[] args) {
// TODO code application logic here
int [] numerosAleatorios = new int [100];
Random random = new Random();
System.out.println("Arreglo aleatorio");
for (int i = 0; i <numerosAleatorios.length; i++)
{
numerosAleatorios[i] = random.nextInt(100)+1;
System.out.println( numerosAleatorios[i]);
}

System.out.println("Arreglo aleatorio con múltiplos de 5");


for (int i = 0; i < numerosAleatorios.length; i++)
{
if (numerosAleatorios[i] % 5 ==0)
{
System.out.println( numerosAleatorios[i]);
}
}

}
4. Dado los siguientes valores, 10, -3, 6, -8, 25, 23, 5, 16, 11, -9, mostrar solamente los valores
negativos
public static void main(String[] args) {
// TODO code application logic here
int [] valores = new int [10];
valores[0]=10;
valores[1]=-3;
valores[2]=6;
valores[3]=-8;
valores[4]=25;
valores[5]=23;
valores[6]=5;
valores[7]=16;
valores[8]=11;
valores[9]=-9;

for (int i = 0; i < valores.length; i++)


{
if (valores[i] < 0)
{
System.out.println(valores[i]);
}

}
}
5. Crear dos arreglos de 10 elementos y cárgalos aleatoriamente de entre 1-200 y luego mostrar
los dos arreglos, más el tercero que es la suma de los elementos de los otros dos arreglos.
public static void main(String[] args) {
// TODO code application logic here
int [] arreglo1 = new int [10];
int [] arreglo2 = new int [10];
int [] arregloSuma = new int [10];
Random random = new Random();
System.out.println("Arreglo 1");
for (int i = 0; i < arreglo1.length; i++)
{
arreglo1[i]= random.nextInt(200)+1;
System.out.println("["+ arreglo1[i]+ "]");
}
System.out.println("Arreglo 2");
for (int i = 0; i < arreglo2.length; i++)
{
arreglo2[i]=random.nextInt(200)+1;
System.out.println("["+ arreglo2[i]+ "]");
}
System.out.println("Suma de arreglos");
for (int i = 0; i < arregloSuma.length; i++)
{
arregloSuma[i]= arreglo1[i] + arreglo2[i];
System.out.println("["+ arregloSuma[i]+ "]");
}

6. Realice un programa que lea 5 números por teclado, los copie a otro array multiplicados por
2 y muestre el segundo array.
public static void main(String[] args) {
// TODO code application logic here

int [] numeros = new int[5];


int [] numerosX2 = new int [5];

Scanner teclado = new Scanner(System.in);

for(int i=0; i<numeros.length; i++)


{
System.out.printf("Introduzca número %d: ", i+1);
numeros[i] = teclado.nextInt();
}
for (int i = 0; i < numerosX2.length; i++)
{
numerosX2[i]= numeros[i]*2;
}
System.out.println("Números : ");
for(int i=0; i<numeros.length; i++)
{
System.out.println(numeros[i]);
}
System.out.println("Números * 2 :");
for(int i=0; i<numerosX2.length; i++)
{
System.out.println(numerosX2[i]);
}

7. Da los siguiente estudiantes: Juan Peres 17, Mario Galindo 40, Grover Sanches 12, Camila
Rosas 20, Roger Varias 22, Marco Dinas 29, Patricia Caceres 8, Pilar Montenegro 11, Fusto
Silas 33, Leny Claros 5, Crear 3 arreglos uno para guardar los nombres, otro los apellidos y el
tercero las edades, luego mostrar el estuiante con menor edad, el estudiante con mayor edad y
la edad promedio.
public static void main(String[] args) {
// TODO code application logic here
String [] nombres = new String [10];
String [] apellidos = new String [10];
int [] edades = new int [10];

nombres [0] = "Juan";


nombres [1] = "Mario";
nombres [2] = "Grover";
nombres [3] = "Camila";
nombres [4] = "Roger";
nombres [5] = "Marco";
nombres [6] = "Patricia";
nombres [7] = "Pilar";
nombres [8] = "Fusto";
nombres [9] = "Leny";

apellidos[0]= "Peres";
apellidos[1]= "Galindo";
apellidos[2]= "Sanches";
apellidos[3]= "Rosas";
apellidos[4]= "Varias";
apellidos[5]= "Dinas";
apellidos[6]= "Caceres";
apellidos[7]= "Montenegro";
apellidos[8]= "Silas";
apellidos[9]= "Claros";

edades [0]=17;
edades [1]=40;
edades [2]=12;
edades [3]=20;
edades [4]=22;
edades [5]=29;
edades [6]=8;
edades [7]=11;
edades [8]=33;
edades [9]=5;

int mayor, menor;


int suma=0;
mayor = menor = edades [0];

for (int i = 0; i < edades.length; i++)


{
if(edades [i] > mayor)
{
mayor = edades[i];
}
if(edades[i]<menor) {
menor = edades[i];
}
}
for (int i = 0; i < edades.length; i++)
{
suma = suma + edades[i];
}
System.out.println("El mayor valor es: "+mayor);
System.out.println("El menor valor es: "+menor);
System.out.println("La edad promedio es: " + (suma/10));
}
8. Dado una matriz de N*M cargadas por teclado realizar la suma de sus elementos, el elemento
mayor y menor.

9. Dado una matriz de N*M cargadas aleatoriamente con calores de entre 1 y 20 obtener su
promedio.
public static void main(String[] args) {
// TODO code application logic here

Random random = new Random();


int [][] numeros = new int [4][5];
int suma=0;
for (int i = 0; i < numeros.length; i++)
{
for (int j = 0; j < numeros[0].length; j++)
{
numeros[i][j] = random.nextInt(20)+1;
suma= suma + numeros[i][j];
System.out.printf("En la posicion %d - %d = %d \n", i, j, numeros[i][j]);
}
}
System.out.println("El promedio de la matriz es: " + (suma/numeros.length));
}

10. Dado dos matrices cuadrada de N*N cargarlos con valores aleatorios de entre 1 y 100, realizar la
suma de sus elementos en sus mismas posiciones, el resultado final es otra matriz de N*N
public static void main(String[] args) {

// TODO code application logic here

Random random = new Random();

int [][] matriz1 = new int [3][3];

int [][] matriz2= new int [3][3];

int [][] matrizSuma = new int [3][3];

for (int i = 0; i < matriz1.length; i++)

for (int j = 0; j < matriz1[0].length; j++)

matriz1[i][j] = random.nextInt(100)+1;

matriz2[i][j] = random.nextInt(100)+1;

matrizSuma[i][j]= matriz1[i][j] + matriz2[i][j];


}

System.out.println("Matriz 1 : ");

for (int i = 0; i < matriz1.length; i++)

for (int j = 0; j < matriz1.length; j++)

System.out.printf("En la posicion %d - %d = %d \n", i, j, matriz1[i][j]);

System.out.println("Matriz 2 : ");

for (int i = 0; i < matriz2.length; i++)

for (int j = 0; j < matriz2.length; j++)

System.out.printf("En la posicion %d - %d = %d \n", i, j, matriz2[i][j]);

System.out.println("Suma de Matrices : ");

for (int i = 0; i < matrizSuma.length; i++)

for (int j = 0; j < matrizSuma.length; j++)

System.out.printf("En la posicion %d - %d = %d \n", i, j, matrizSuma[i][j]);

}
}

También podría gustarte