EXPERIMENT-1
Develop a JAVA program to add TWO matrices of
suitable order N (The value of N should be read
from command line arguments).
import [Link];
// Import Scanner class to take input from the user
class AddMatrix
{
public static void main(String args[])
{
int row, col, i, j;
// row = number of rows
// col = number of columns
// i, j = loop counters
Scanner in = new Scanner([Link]);
// Create Scanner object for input
[Link]("Enter the number of rows");
row = [Link]();
// Read number of rows
[Link]("Enter the number columns");
col = [Link]();
// Read number of columns
int mat1[][] = new int[row][col];
int mat2[][] = new int[row][col];
int res[][] = new int[row][col];
// Create 3 matrices of size row x col
[Link]("Enter the elements of matrix1");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
mat1[i][j] = [Link]();
[Link]();
}
// Read elements into mat1
[Link]("Enter the elements of matrix2");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
mat2[i][j] = [Link]();
[Link]();
}
// Read elements into mat2
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
res[i][j] = mat1[i][j] + mat2[i][j];
// Add both matrices element-wise and store in res
[Link]("Sum of matrices:-");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
[Link](res[i][j] + "\t");
[Link]();
}
// Print the result matrix
}
}
OUTPUT:
Enter the number of rows
2
Enter the number columns
3
Enter the elements of matrix1
123
456
Enter the elements of matrix2
567
893
Sum of matrices:-
6 8 10
12 14 9