0% found this document useful (0 votes)
44 views2 pages

Class Rotatematrix: 17 Dec, 2018 12:25:26 Am

The document describes a program that accepts user input to define the size of a square matrix, inputs values into the matrix, displays the original matrix, rotates the matrix 90 degrees clockwise, and calculates the sum of the elements in the four corners of the matrix. The program first checks that the matrix size is between 3x3 and 9x9, then inputs the elements, displays the original matrix, rotates the matrix by printing the columns of the original matrix in reverse order as rows, and finally calculates the sum of the corner elements.

Uploaded by

Seema Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

Class Rotatematrix: 17 Dec, 2018 12:25:26 Am

The document describes a program that accepts user input to define the size of a square matrix, inputs values into the matrix, displays the original matrix, rotates the matrix 90 degrees clockwise, and calculates the sum of the elements in the four corners of the matrix. The program first checks that the matrix size is between 3x3 and 9x9, then inputs the elements, displays the original matrix, rotates the matrix by printing the columns of the original matrix in reverse order as rows, and finally calculates the sum of the corner elements.

Uploaded by

Seema Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Class RotateMatrix 1/2

/**
* Write a program to declare a square matrix A[ ][ ] of order MxM 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) Rotate the matrix 90° clockwise.
(c) Find the sum of the elements of the four corners of the matrix.
*/

import java.util.*;
class RotateMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();

if(m<3 || m>9)
System.out.println("Size Out Of Range");
else
{
int A[][]=new int[m][m];

/* Inputting the matrix */


for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}

/* Printing the original matrix */


System.out.println("*************************");
System.out.println("The Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}

17 Dec, 2018 12:25:26 AM


Class RotateMatrix (continued) 2/2

System.out.println("*************************");

/*Rotation of matrix begins here */


System.out.println("Matrix After Rotation is : ");
for(int i=0;i<m;i++)
{
for(int j=m-1;j>=0;j--)
{
System.out.print(A[j][i]+"\t");
}
System.out.println();
}
System.out.println("*************************");

int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding su


m of corner elements
System.out.println("Sum of the corner elements = "+sum);
}
}
}
/** OUTPUT
* Enter the size of the matrix : 3
Enter an element : 11
Enter an element : 19
Enter an element : 63
Enter an element : 8
Enter an element : 24
Enter an element : 0
Enter an element : 32
Enter an element : 13
Enter an element : 9
*************************
The Original Matrix is :
11 19 63
8 24 0
32 13 9
*************************
Matrix After Rotation is :
32 8 11
13 24 19
9 0 63
*************************
Sum of the corner elements = 115

*/

17 Dec, 2018 12:25:27 AM

You might also like