You are on page 1of 2

package me.

parzibyte;

public class Main {

static void imprimirMatriz(int[][] matriz) {

for (int y = 0; y < matriz.length; y++) {

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

System.out.printf("%d ", matriz[y][x]);

System.out.println();

static void sumaFilas(int[][] matriz) {

for (int y = 0; y < matriz.length; y++) {

int suma = 0;

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

System.out.printf("%d ", matriz[y][x]);

suma += matriz[y][x];

System.out.printf("= %d\n", suma);

static void sumaColumnas(int[][] matriz) {

// Imprimir la matriz normalmente

imprimirMatriz(matriz);

// Debajo de ella imprimir una línea divisora

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

System.out.print("___");

System.out.println();

// Luego recorrer por columna y sumar

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

int suma = 0;

for (int y = 0; y < matriz.length; y++) {

suma += matriz[y][x];

System.out.printf("%d ", suma);

System.out.println();

}
public static void main(String[] args) {

// https://parzibyte.me/blog

int[][] matriz = {

{20, 12, 77},

{50, 12, 89},

{10, 44, 15},

};

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

imprimirMatriz(matriz);

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

sumaFilas(matriz);

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

sumaColumnas(matriz);

run:

La matriz es:

20 12 77

50 12 89

10 44 15

Suma de filas:

20 12 77 = 109

50 12 89 = 151

10 44 15 = 69

Suma de columnas:

20 12 77

50 12 89

10 44 15

_________

80 68 181

BUILD SUCCESSFUL (total time: 0 seconds)

You might also like