Advertisement
LightProgrammer000

Media [diagonal principal]

Apr 8th, 2020
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. package Tarefas_3;
  2.  
  3. public class EX_02
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         int linha = 10, coluna = 10;
  8.         int matriz [][] = new int [linha][coluna];
  9.        
  10.         System.out.printf("\n # Media da diagonal principal: %.2f \n", (soma_diagonal_principal(linha, coluna, matriz) / linha) );    
  11.     }
  12.  
  13.     private static double soma_diagonal_principal(int linha, int coluna, int [][] matriz)
  14.     {
  15.         int num = 0, soma = 0;
  16.        
  17.         // Estrutura de repeticao: Matriz 10 x 10
  18.         for (int i = 0; i < linha; i++)
  19.         {
  20.             for (int j = 0; j < coluna; j++)
  21.             {
  22.                 matriz[i][j] = ++num;
  23.                 System.out.print("\t" + matriz[i][j]);
  24.  
  25.                 if (i == j)
  26.                 {
  27.                    soma += matriz[i][j];
  28.                 }
  29.             }
  30.  
  31.             System.out.println("");
  32.         }
  33.  
  34.         return soma;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement