-
-
Save parzibyte/07e8d4c1e3d5c7353838d785634e841f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.parzibyte; | |
// https://parzibyte.me/blog | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment