Saturday, August 24, 2019

BlueJ Program to check for identity matrix


A matrix is known as identity matrix in which all diagonal elements are 1. In this BlueJ program, a matrix will be checked whether it is identity matrix or not.

import java.util.*;
class Search
{
    int i,j;
    int arr[][]=new int[5][5];
    Scanner sc=new Scanner(System.in);
public void take()
{
     for(int i=0;i< =4;i++)
    {
          for(j=0;j< =4;j++)
           {
               System.out.print("\nValue: ");
                arr[i][j]=sc.nextInt();
            }
        }
      
    }
    public void show()
    {
        int x=0;
       
        System.out.println("\nEntered Matrix Values.");
        for(i=0;i< =4;i++)
         {
              for(j=0;j< =4;j++)
              {
                  System.out.print(" "+arr[i][j]);
                }
                System.out.println();
            }
         for(i=0;i< =4;i++)
         {
              for(j=0;j< =4;j++)
              {
                  if(i==j && arr[i][j]!=1 || i+j==4 && arr[i][j]!=1)
                  x=1;
                }
            }
            if(x==0)
             System.out.print("\nIdentity Matrix.");
             else
            System.out.print("\nNot Identity Matrix.");
}
      
    public static void main(String args[])
    {
        Search ob=new Search();
        ob.take();
        ob.show();
   }
 }

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner