Skip to content

Instantly share code, notes, and snippets.

@valchaqui
Created November 18, 2015 13:51
Show Gist options
  • Save valchaqui/6f0346c34047a9dd97a4 to your computer and use it in GitHub Desktop.
Save valchaqui/6f0346c34047a9dd97a4 to your computer and use it in GitHub Desktop.
import java.util.*;
public class matriz{
public static void main(String[] args){
int matriz[][] = new int [3][3];
int i,j, soma1=0, soma2=0, pos=3;
Scanner leia = new Scanner(System.in);
System.out.print("Entre com os valores da matriz\n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
System.out.print("Numero da linha "+(i+1)+" e da coluna "+(j+1)+ ":");
matriz[i][j]=leia.nextInt();
if(i==j){
soma1=soma1+matriz[i][j];
}
if((i+j)==2){
soma2=soma2+matriz[i][j];
}
}
}
System.out.print("\n\nMatriz:\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
System.out.print(" "+matriz[i][j]+ " ");
}
System.out.print("\n");
}
System.out.print("A soma da diagonal principal é "+soma1+"\n\n");
System.out.print("A soma da diagonal secundaria é "+soma2+"\n\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment