Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 24, 2021 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/07e8d4c1e3d5c7353838d785634e841f to your computer and use it in GitHub Desktop.
Save parzibyte/07e8d4c1e3d5c7353838d785634e841f to your computer and use it in GitHub Desktop.
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