Skip to main content

Write a program to accept a 3x3 matrix. Display the square of the product of the left and right diagonal sum of the matrix.

Write a program to accept a 3x3 matrix. Display the square of the product of the left and right diagonal sum of the matrix.
Input Matrix:
1   2    3
4   5    6
7   8    9
Output :  
Product of the left diagonal = 1*5*9=45
Product of the right diagoal = 3*5*7 = 105
Sum of the products 45+105 = 150
Square of the sum = 12.2


import java.util.Scanner;

class matrixsquare

{

public static void main(String args[])

{

int A[][] = new int[3][3]; // 3 rows and 3 columns

Scanner sc=new Scanner(System.in);

System.out.println("Enter numbers into the matrix:");

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

A[i][j]=sc.nextInt();

}

}

int ld=1,rd=1;

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

ld=ld*A[i][i]; //left diagonal product

rd=rd*A[i][3-i-1]; //right diagoal product 

}

}

int sp = ld+rd; //sum of the product 

double sq = Math.sqrt(sp);

System.out.println("The left diagoal product is: "+ld);

System.out.println("The right diagoal product is: "+rd);

System.out.println("Sum of the product is:"+sp);

System.out.println("Square of the product is:"+sq);

}

}

Popular posts from this blog