0% found this document useful (0 votes)
9 views7 pages

Java Programs for Matrix Operations

The document contains five Java programs that demonstrate various operations on 2D arrays, including finding the maximum and minimum values, transposing a matrix, creating a mirror image of a matrix, checking for symmetry, and determining if a matrix is unique based on boundary sums. Additionally, it includes a program to check if a matrix is special by comparing the sums of its diagonals. Each program is structured with input prompts and outputs results based on user-provided data.
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)
9 views7 pages

Java Programs for Matrix Operations

The document contains five Java programs that demonstrate various operations on 2D arrays, including finding the maximum and minimum values, transposing a matrix, creating a mirror image of a matrix, checking for symmetry, and determining if a matrix is unique based on boundary sums. Additionally, it includes a program to check if a matrix is special by comparing the sums of its diagonals. Each program is structured with input prompts and outputs results based on user-provided data.
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

Program 1

// Write a program to enter numbers in a double dimensional array of size 3*3 and display maximum
and minimum number present in the array.
import [Link].*;
public class min_max_dda
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int a[][]=new int[3][3];
[Link]("enter the array element");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=[Link]();
}
}
int max=a[0][0],min=a[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(a[i][j]>max)
max=a[i][j];
if(a[i][j]<min)
min=a[i][j];
}}
[Link]("the maximum number is"+max);
[Link]("the minimum number is"+min);
}
}
Program 2

/* write a program to create a matrix of size 3*3 and find the transpose of the matrix.
* Transpose of a matrix is obtained by changing rows to columns and columns to rows.

Matrix
134
243
345
Printing Matrix After Transpose:
123
344
435
*/
import [Link].*;
public class transpose
{
public static void main(String args[])
{
Scanner sc= new Scanner([Link]);
int a[][]=new int[3][3];

[Link]("Enter array elements");


for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=[Link]();
}
}

[Link]("Matrix");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
[Link](a[i][j]+"\t ");
}
[Link]();
}
[Link]("Printing Matrix After Transpose:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
[Link](a[j][i]+" ");
}
[Link]();
}
}
}
Program 3

/* Write a program in Java to enter natural numbers in a double dimensional array m x n


* (where m is the number of rows and n is the number of columns).
* Display the new matrix in such a way that the new matrix is
* the mirror image of the original matrix.
8 15 9 18
9 10 7 6
10 8 11 13
12 16 17 19
Sample Input
18 9 15 8
6 7 10 9
13 11 8 10
19 17 16 12
Sample Output
*/
import [Link].*;
public class MirrorImage
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows m ");
int m = [Link]();
[Link]("Enter number of columns n ");
int n = [Link]();
int a[][] = new int[m][n];

[Link]("Enter array elements");


for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = [Link]();
}
}

[Link](" Matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
[Link](a[i][j] + "\t");
}
[Link]();
}

[Link]("Mirror Image of Matrix :");


for (int i = 0; i < m; i++)
{
for (int j = n-1; j >= 0; j--)
{
[Link](a[i][j] + "\t");
}
[Link]();
}
}
}
Program 4
/* Write a program to declare a square matrix a[ ] [ ] of order (M x M)
(a) Display the original matrix.
(b) Check if the given matrix is 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.
123
245
356
OUTPUT :
ORIGINAL MATRIX
123
245
356
*/
import [Link].*;
class Symetric
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter m ");
int m = [Link]();
int a[][]=new int[m][m];

[Link]("Enter elements in the Matrix");


for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=[Link]();
}
}

/* Printing the Original Matrix */


[Link]("The Original Matrix is ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
[Link](a[i][j]+"\t");
}
[Link]();
}

/* Checking whether the matrix is symmetric or not */


int flag = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j] != a[j][i])
{
flag = 1; // Setting flag = 1 when elements do not match
break;
}
}
}

if(flag == 1)
[Link]("The given Matrix is Not Symmetric");
else
[Link]("The given Matrix is Symmetric");

}
}
Program 5

/* write a program to create a DDA of size n*n and check whether the given matrix is a UNIQUE
matrix or not
* a UNIQUE matrix is a matrix where sum of boundary and non boundary are same*/
import [Link].*;
public class unique_matrix
{
public static void main(String args[])
{
Scanner sc=new Scanner ([Link]);
[Link]("ENTER n");
int n=[Link]();
int a[][]=new int[n][n];
[Link]("ENTER ARRAY ELEMENTS");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=[Link]();
}
}
int sumb=0,sumnb=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==0||j==0||i==n-1||j==n-1)
sumb=sumb+a[i][j];
else
sumnb=sumnb+a[i][j];
}
}
if(sumb==sumnb)
[Link]("UNIQUE MATRIX");
else
[Link]("NOT UNIQUE MATRIX");
}
}

Question 6
[15]
Define a class to accept values into an integer array of order n*n and check whether it is a
special matrix or not. An array is special if the sum of the left diagonal elements equals
the sum of the right diagonal elements. Print the appropriate message.
Example:
3 4 2 5 Sum of the left diagonal elements =
2 5 2 3 3 + 5 + 2 + 1 = 11
5 3 2 7 Sum of the right diagonal elements =
1 3 7 1 5 + 2 + 3 + 1 = 11

import [Link].*;
public class special
{
public static void main(String args[])
{
Scanner sc=new Scanner ([Link]);
[Link]("ENTER n");
int n=[Link]();
int a[][]=new int[n][n];
[Link]("ENTER ARRAY ELEMENTS");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=[Link]();
}
}
[Link]("Matrix");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
[Link](a[i][j]+"\t ");
}
[Link]();
}
int suml=0,sumr=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
suml=suml+a[i][j];
if(i+j==n-1)
sumr=sumr+a[i][j];
}
}
if(sumr==suml)
[Link]("SPECIAL MATRIX");
else
[Link]("NOT SPECIAL MATRIX");
}
}

You might also like