0% found this document useful (0 votes)
15 views

Lab

Lab Questions Of class 12th isc

Uploaded by

supriyasingh8474
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab

Lab Questions Of class 12th isc

Uploaded by

supriyasingh8474
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM QUESTION

Write a program to declare a square matrix A[][] of order M × M where ‘M’ is the number of
rows and the number of columns, such that M must be greater than 2 and less than 10.

Accept the value of M as user input. Display an appropriate message for an invalid input. Allow
the user to input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Check if the given matrix is said to be symmetric or not. A square matrix is said to be
symmetric, if the element of the ith row and jth column is equal to the element of the jth row and
ith column.
(c) Find the sum of the elements of left diagonal and the sum of the elements of right diagonal
of the matrix and display them.

Example 1
INPUT:
M=3
Enter elements of the matrix:
123
245
356
OUTPUT:
ORIGINAL MATRIX
123
245
356
THE GIVEN MATRIX IS SYMMETRIC.
THE SUM OF THE LEFT DIAGONAL = 11
THE SUM OF THE RIGHT DIAGONAL = 10

Example 2
INPUT:
M=4
Enter elements of the matrix:
7892
4563
8531
7642
OUTPUT:
ORIGINAL MATRIX
7892
4563
8531
7642
THE GIVEN MATRIX IS NOT SYMMETRIC.
THE SUM OF THE LEFT DIAGONAL = 17
THE SUM OF THE RIGHT DIAGONAL = 20
Example 3
INPUT: M = 12
OUTPUT: SIZE IS OUT OF RANGE.

SOLUTION WITH COMMENTS


import java.util.*;
class symmetric
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of array=");
int m=sc.nextInt();
if(m<3 || m>10) // checking (M must be greater than 2 and less than 10) is not followed
{
System.out.println("THE MATRIX SIZE IS OUT OF GIVEN RANGE.");
System.exit(0); // stopping the program if the size of matrix is not in the given range
}
int a[][]=new int [m][m];

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


{
for(int j=0;j<m; j++)
{
System.out.print("Enter elements in Matrix=");
a[i][j]=sc.nextInt(); // entering the elements in matrix
}
}
System.out.println("your matrix=");
for(int i=0;i<m; i++)
{
for(int j=0;j<m; j++)
{
System.out.print(a[i][j]); // printing the matrix
}
System.out.println(); // changing the row of matrix for printing
}
int flag = 0; //flag remains 0 if all the elements of the matrix is symmetric
for (int i=0; i<m; i++)
{
for (int j=0; j<m; j++)
{
if (a[i][j]!=a[j][i]) // checking if any element of matrix is not symmetric
{
flag =1; // if any element is not symmetric flag becomes 1
break; // stopping the loop if the element is not symmetric
}
}
}
if(flag==0)
{
System.out.println("SYMMERTIC MATRIX");
}
else
{
System.out.println("NOT SYMMERTIC MATRIX");
}
int sl=0; //variable for adding left diagonal
int sr=0; //variable for adding right diagonal
for (int i=0; i<m; i++)
{
for (int j=0; j<m; j++)
{
if (i==j) // left diagonal condition
{
sl=sl+a[i][j]; //left diagonal sum
}
if ((i+j) == (m-1))// right diagonal condition
{
sr=sr+a[i][j]; //right diagonal sum
}
}
}
System.out.println("LEFT DIAGONAL="+sl); // printing sum of left diagonal
System.out.println("RIGHT DIAGONAL="+sr); // printing sum of right diagonal
}
}

You might also like