0% found this document useful (0 votes)
6 views4 pages

Java Matrix Operations Example

The document contains a Java program that creates a matrix based on user-defined dimensions and performs operations on it. It includes methods for reading a matrix, visualizing it, and calculating the sum of its diagonals if the matrix is square. The program outputs the matrix and the sum of its diagonals after execution.

Uploaded by

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

Java Matrix Operations Example

The document contains a Java program that creates a matrix based on user-defined dimensions and performs operations on it. It includes methods for reading a matrix, visualizing it, and calculating the sum of its diagonals if the matrix is square. The program outputs the matrix and the sum of its diagonals after execution.

Uploaded by

jeff yupanqui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

package PracticaMatrices;

import [Link];

/**

* @author YUPANQUI CASTELLANOS

*/

public class Matrices {

public static void main(String [] args){

Scanner leer = new Scanner([Link]);

int matriz[][];

int filas;

int columnas;

[Link]("FILAS: ");

filas = [Link]();

[Link]("COLUMNAS: ");

columnas = [Link]();

[Link]("");

matriz = new int[filas][columnas];

Operaciones obj = new Operaciones();

obj.leer_matriz(matriz);

obj.visualizar_matriz(matriz);

obj.sumar_diagonales(matriz, filas, columnas);

}
}

package PracticaMatrices;

/**

* @author YUPANQUI CASTELLANOS

*/

public class Operaciones {

void leer_matriz(int [][] matriz){

for(int i=0; i<[Link]; i++){

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

matriz[i][j] = (int) ([Link]()*10+1);

void visualizar_matriz(int [][] matriz){

for(int i=0; i<[Link]; i++){

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

[Link](matriz[i][j]+"\t");

[Link]("");

}
}

void sumar_diagonales(int [][] matriz, int filas, int columnas){

int suma_prin = 0;

int suma_sec = 0;

int suma_diag;

if(filas == columnas){

for(int i=0; i<[Link]; i++){

suma_prin += matriz[i][i];

for(int i=0; i<[Link]; i++){

suma_sec += matriz[i][[Link]-i-1];

suma_diag = suma_prin +suma_sec;

[Link]("");

[Link]("SUMA DIAGONALES: "+ suma_diag);

else{

[Link]("LA MATRIZ NO ES CUADRADA");

}
COMPILACION:

run:

FILAS: 3

COLUMNAS: 3

4 1 6

8 3 9

7 6 8

SUMA DIAGONALES: 31

BUILD SUCCESSFUL (total time: 4 seconds)

You might also like