Os Module4
Os Module4
Lab Programs
1. Develop a JAVA program to add TWO matrices of suitable order N (The value of N
should be read from command line arguments).
package oopswjava;
import java.util.Scanner;
if (p == m && q == n)
{
int a[][] = new int[p][q];
int b[][] = new int[m][n];
int c[][] = new int[m][n];
System.out.println("Enter all the "+p*q+ " elements of first matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = s.nextInt();
}
}
System.out.println("Enter all the "+m*n+" elements of second matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = s.nextInt();
}
}
System.out.println("First Matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
System.out.println("Second Matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println("");
}
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Matrix after addition:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
else
{
System.out.println("Addition would not be possible as the order of the matrices does not
match");
}
s.close();
}
}
Output:
Run 1:
Program arguments: 3 2 3 4
Run 2:
Program arguments: 3 3 3 3