Skip to main content

Program in Java: To store values into 3x3 matrix. Find the sum of the left diagonal and right diagonal of the matrix. If the left and right sum of the matrix is equal then display cross matrix other not cross matrix.

 Program in Java: To store values into 3x3 matrix. Find the sum of the left diagonal and right diagonal of the matrix. If the left and right sum of the matrix is equal then display cross matrix other not cross matrix.


import java.util.Scanner;
class corss
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 3x3 matrix:");
        int A[][] = new int[3][3];
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                A[i][j]=sc.nextInt();
            }
        }
        int rd=0,ld=0; //right and left diagonal
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                ld=ld+A[i][i];
                rd=rd+A[i][3-i-1];
            }
        }
        System.out.println("Sum of left diagonal is:"+ld);
        System.out.println("Sum of right diagonal is:"+rd);
        if(ld==rd)
            System.out.println("Cross Matrix");
        else
            System.out.println("Non-Cross Matrix");
    }
}

Popular posts from this blog