ALL MATRICES PROGRAMS IN JAVA
ALL MATRICES PROGRAMS IN JAVA
SHOWING
ELEMENTS OF MATRIX :
import java.util.Scanner;
public class ShowMatrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = new int[3][3];
int i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter values of matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Given matrix is = ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"");
}
System.out.println("");
}
}
} 
Output :
Enter values of matrix
1
2
3
4
5
6
7
8
9
Given matrix is =
123
456
789
SUM OF
TWO MATRICES
import java.util.Scanner;
public class Sum_Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int c[][] = new int[3][3];
int i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Element of matrix A =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("ENter Element of matrix B =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j] = sc.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Sum of given matrix is = ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println("");
}
}
}
OUTPUT :
Enter Element of matrix A =
1
2
3
1
2
3
1
2
3
ENter Element of matrix B =
1
2
3
1
2
3
1
2
3
Sum of given matrix is =
2 4 6
2 4 6
2 4 6
MULTIPLICATION
OF TWO MATRICES :
import java.util.Scanner;
public class Multi_Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int c[][] = new int[3][3];
int i,j,k;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Element of matrix A =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("ENter Element of matrix B =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j] = sc.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j] = 0;
for(k=0;k<3;k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
}
}
}
System.out.println("Multiplication of Matrix A and B =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println("");
}
}
}
OUTPUT :
Enter Element of matrix A =
1
2
3
1
2
3
1
2
3
ENter Element of matrix B =
1
2
3
1
2
3
1
2
3
Multiplication of Matrix A and B =
6 12 18
6 12 18
6 12 18
Square
of a matrix :
import java.util.Scanner;
public class Square_Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = new int[3][3];
int c[][] = new int[3][3];
int i,j,k;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Element of matrix
=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j] = sc.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j] = 0;
for(k=0;k<3;k++)
{
c[i][j] = c[i][j]+(a[i][k]*a[k][j]);
}
}
}
System.out.println("Square of matrix is =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println("");
}
}
}
OUTPUT :
Enter Element of matrix =
1
2
3
4
5
6
7
8
9
Square of matrix is =
30 36 42
66 81 96
102 126 150
TRANSPOSE OF A MATRIX :
import java.util.Scanner;
public class Transpose_Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = new int[3][3];
int t[][] = new int[3][3];
int i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Element of matrix
=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j] = sc.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
t[i][j] = a[j][i];
}
}
System.out.println("Entered matrix is =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println("");
}
System.out.println("Transpose Matrix is =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(t[i][j]+"\t");
}
System.out.println("");
}
}
}
OUTPUT :
Enter Element of matrix =
1
2
3
4
5
6
7
8
9
Entered matrix is =
1 2 3
4 5 6
7 8 9
Transpose Matrix is =
1 4 7
2 5 8
3 6 9
CHECK
FOR A SYMMETRIC MATRIX :
import java.util.Scanner;
public class Symmetric_Matrix {
public static void main(String[] args) {
int a[][] = new int[3][3];
int b[][] = new int[3][3];
boolean f;
int i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Element of
matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Given matrix is = ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println("");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j] = a[j][i];
}
}
System.out.println("Transpose of the given
matrix is");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println("");
}
f=true;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]!=b[i][j]);
{
f=false;
break;
}
}
}
if(f)
{
System.out.println("Matrix is symmetric");
}
else
{
System.out.println("matrix is not
symmetric");
}
}
}
OUTPUT :
Enter the Element of matrix
1
2
1
2
3
4
3
4
3
Given matrix is =
1 2 1
2 3 4
3 4 3
Transpose of the given matrix is
1 2 3
2 3 4
1 4 3
matrix is not symmetric
Determinant
of a Matrix :
import java.util.Scanner;
public class Determinant_Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = new int[3][3];
int det;
int i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Element of matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Entered Matrix is =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"");
}
System.out.println("");
}
det = a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2])-a[0][1]*(a[1][0]*a[2][2]-a[2][0]*a[1][2])+a[0][2]*(a[1][0]*a[2][1]-a[2][0]*a[1][1]);
System.out.println("Determinant of matrix is "+det);
}
}
Output :
Enter Element of matrix
1
2
1
4
0
2
3
5
1
Entered Matrix is =
121
402
351
Determinant of matrix is 14
CHECK
FOR A SPARSE MATRIX :
import java.util.Scanner;
public class Sparse_Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int i,j;
int a[][] = new int[3][3];
int cot =0;
System.out.println("Enter Element of matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j] = sc.nextInt();
if(a[i][j]==0)
{
cot ++;
}
}
}
System.out.println("Entered matrix is =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"");
}
System.out.println("");
}
if(cot
>(3*3)/2)
{
System.out.println("the entered matrix is sparse matrix");
}
else
{
System.out.println("Enterd matrix is not sparse matrix");
}
}
}
OUTPUT :
Enter Element of matrix
1
2
0
0
0
3
0
0
5
Entered matrix is =
120
003
005
the entered matrix is sparse matrix

Nice work for your better
ReplyDeleteKnowledge.
Thanks 🤗
DeleteIs C & C++ is necessary to learn Java ?
ReplyDeleteyes,
Deletebut if you have an idea on computer programming then to learn java no need to learn C or C++...