0% found this document useful (0 votes)
11 views3 pages

Matrices

The document contains 3 Java programs that perform operations on 2D arrays (matrices). The first program fills a matrix with prime numbers. The second program takes user input to fill a matrix, calculates the sum and average of elements. The third program modifies a user-input matrix by changing elements above and below the diagonal.

Uploaded by

David Garnica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Matrices

The document contains 3 Java programs that perform operations on 2D arrays (matrices). The first program fills a matrix with prime numbers. The second program takes user input to fill a matrix, calculates the sum and average of elements. The third program modifies a user-input matrix by changing elements above and below the diagonal.

Uploaded by

David Garnica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Ejercicio_1

import java.util.Scanner;

public class Ejercicio_1 {

public static void main(String[] args) {


Scanner tec=new Scanner(System.in);
System.out.println("Ingrese el numero de filas: ");
int n=tec.nextInt();
System.out.println("Ingrese el numero de columnas: ");
int m=tec.nextInt();
int [][]matriz=new int[n][m];

int numero = 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
while (!esPrimo(numero)) {
numero++;
}
matriz[i][j] = numero;
numero++;
}
}
System.out.println("La matriz rellenada con los primeros numeros primos
es:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(matriz[i][j] + " ");
}
System.out.println();
}
}
public static boolean esPrimo(int numero) {
if (numero <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(numero); i++) {
if (numero % i == 0) {
return false;
}
}
return true;
}
}

Ejercicio_2

import java.util.Scanner;
public class Ejercicio_2 {
public static void main(String[] args) {
Scanner tec = new Scanner(System.in);

System.out.print("Ingrese el numero de filas: ");


int n = tec.nextInt();

System.out.print("Ingrese el numero de columnas: ");


int m = tec.nextInt();
int[][] matriz = new int[n][m];

for (int i = 0; i < n; i++) {


for (int j = 0; j < m; j++) {
System.out.print("Ingrese el valor de la matriz: " );
matriz[i][j] = tec.nextInt();
}
}

System.out.println("La matriz ingresada es:");


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(matriz[i][j] + " ");
}
System.out.println();
}

int suma = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
suma += matriz[i][j];
}
}
double promedio = (double) suma / (n * m);

System.out.println("El promedio de los valores de la matriz es: " +


promedio);

}
}

Ejercicio_3
import java.util.Scanner;
public class Ejercicio_3 {
public static void main(String[] args) {
Scanner tec = new Scanner(System.in);
System.out.print("Ingrese el numero de filas: ");
int n = tec.nextInt();
System.out.print("Ingrese el numero de columnas: ");
int m = tec.nextInt();
int[][] matriz = new int[n][m];
System.out.println("Ingrese los elementos de la matriz:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int elemento;
do {
System.out.print("Ingrese el elemento de la matriz: ");
elemento = tec.nextInt();
} while (elemento < 0);
matriz[i][j] = elemento;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i > j) {
if (matriz[i][j] >= 10) {
matriz[i][j] = matriz[i][j] % 10;
}
} else if (i < j) {
if (matriz[i][j] < 10) {
matriz[i][j] += 10;
}
} else {
matriz[i][j] = 1;
}
}
}
System.out.println("La matriz resultante es:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(matriz[i][j] + "\t");
}
System.out.println();
}
}
}

You might also like