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

Java Matrix

This Java code defines a matrix class and performs several operations on matrices including addition, transposition, scalar multiplication, and matrix multiplication. It takes user input to define the sizes of two matrices, fills them with values, and performs the specified operation on the matrices depending on their dimensions. The results are printed to the console. It checks that matrix addition and multiplication can only be done if the dimensions are compatible.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Matrix

This Java code defines a matrix class and performs several operations on matrices including addition, transposition, scalar multiplication, and matrix multiplication. It takes user input to define the sizes of two matrices, fills them with values, and performs the specified operation on the matrices depending on their dimensions. The results are printed to the console. It checks that matrix addition and multiplication can only be done if the dimensions are compatible.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

public class matrix{

public static void main(String[] args){

int[][] matrix1=new int[10][10];


int[][] matrix2=new int[10][10];
int[][] answer=new int[10][10];
Scanner in=new Scanner(System.in);
int row1, col1, row2, col2, n;
System.out.println("Enter number row of Matrix 1:");
row1=in.nextInt();
System.out.println("Enter number col of Matrix 1:");
col1=in.nextInt();
System.out.println();
System.out.println("Enter number of row of Matrix 2:");
row2=in.nextInt();
System.out.println("Enter number of col of Matrix 2:");
col2=in.nextInt();

if((row1==row2)&&(col1==col2)){
System.out.println("Matrix 1:");
for(int x=0; x<row1; x++){
for(int y=0; y<col1; y++){
System.out.print("Matrix1["+ x +"]" +"[" + y +"]");
matrix1[x][y]=in.nextInt();

}
}
System.out.println("Matrix 2:");
for(int x=0; x<row2; x++){
for(int y=0; y<col2; y++){
System.out.print("Matrix2["+ x +"]" +"[" + y +"]");
matrix2[x][y]=in.nextInt();

}
}
for(int x=0; x<row2; x++){
for(int y=0; y<col2; y++){

answer[x][y]=matrix1[x][y] + matrix2[x][y];

}
}
for(int x=0; x<row2; x++){
System.out.println();
for(int y=0; y<col2; y++){

System.out.print(answer[x][y]+ " ");

}
}

System.out.println("Transposition");
System.out.println("Matrix 1");
for(int x=0; x<row1; x++){
System.out.println();
for(int y=0; y<col1; y++){
System.out.print(matrix1[y][x]+ " ");
}
}

System.out.println("Matrix 2");
for(int x=0; x<row2; x++){
System.out.println();
for(int y=0; y<col2; y++){
System.out.print(matrix2[y][x]+" ");
}
}

System.out.println("Enter number:");
n=in.nextInt();
System.out.println("Scalar Multiplication:");
for(int x=0; x<row1; x++){
for(int y=0; y<col1; y++){
answer[x][y]= n * matrix1[x][y];
}
}

for(int x=0; x<row1; x++){


System.out.println();
for(int y=0; y<col1; y++){
System.out.print(answer[x][y]+ " ");
}
}
}
else{
System.out.print("Can't perform Addition of Matrix");

if(col1==row2) {
System.out.println("Matrix 1:");
for(int x=0; x<row1; x++){
for(int y=0; y<col1; y++){
System.out.print("Matrix1["+ x +"]" +"[" + y +"]");
matrix1[x][y]=in.nextInt();

}
}
System.out.println("Matrix 2:");
for(int x=0; x<row2; x++){
for(int y=0; y<col2; y++){
System.out.print("Matrix2["+ x +"]" +"[" + y +"]");
matrix2[x][y]=in.nextInt();

}
}

for(int x=0; x<row1; x++){


for(int y=0; y<col2; y++){
answer[x][y]=0;
}
}

for(int x=0; x<row1; x++){


System.out.println();
for(int y=0; y<col2; y++){
System.out.print(answer[x][y]+ " ");
}
}

for(int i=0; i<row1; i++) {


for(int j=0; j<col2; j++){
for(int k=0; k<col1; k++){
answer[i][j]+=matrix1[i][k] * matrix2[k][j];
}
}
}
for(int x=0; x<row1; x++){
System.out.println();
for(int y=0; y<col2; y++){
System.out.print(answer[x][y]+ " ");
}
}

}
else{
System.out.print("Can't Perform Multiplication of Matrix");

}
}
}

You might also like