Advertisement
Guest User

Nomor 2

a guest
Nov 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. package nomor2;
  2.  
  3. public class Nomor2
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         int[][] arr = {{1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}};
  8.         int countOtherNum = 0;
  9.         int countOne = 0;
  10.        
  11.         //Print array
  12.         for(int i=0;i<arr.length;i++)
  13.         {
  14.             for(int j=0;j<arr[0].length;j++)
  15.             {
  16.                 System.out.print(arr[i][j] + " ");
  17.             }
  18.          
  19.             System.out.println();
  20.         }
  21.        
  22.         //Check if the matrix is identity matrix
  23.         for(int i=0;i<arr.length;i++)
  24.         {
  25.             for(int j=0;j<arr[0].length;j++)
  26.             {
  27.                 if(j != i)
  28.                 {
  29.                     if(arr[i][j] != 0)
  30.                     {
  31.                         countOtherNum++;
  32.                     }
  33.                 } else
  34.                 {
  35.                     if(arr[i][j] == 1)
  36.                     {
  37.                         countOne++;
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.        
  43.         if(countOne == arr.length && countOtherNum == 0)
  44.         {
  45.             System.out.println("Matriks identitas");
  46.         } else
  47.         {
  48.             System.out.println("Bukan matriks identitas");
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement