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

Program 1- JAVA

The document contains a JAVA program that adds two matrices of user-defined dimensions. It prompts the user to input the number of rows and columns for both matrices, checks if they are compatible for addition, and then performs the addition if they are. The program outputs the resulting matrix or an error message if the dimensions do not match.

Uploaded by

gotak78705
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)
6 views

Program 1- JAVA

The document contains a JAVA program that adds two matrices of user-defined dimensions. It prompts the user to input the number of rows and columns for both matrices, checks if they are compatible for addition, and then performs the addition if they are. The program outputs the resulting matrix or an error message if the dimensions do not match.

Uploaded by

gotak78705
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
You are on page 1/ 4

Program 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 java.util.Scanner;

class Add_Matrix

public static void main(String[] args)

int p, q, m, n;

Scanner s = new Scanner(System.in);

System.out.print("Enter number of rows in first matrix:");

p = s.nextInt();

System.out.print("Enter number of columns in first matrix:");

q = s.nextInt();

System.out.print("Enter number of rows in second matrix:");

m = s.nextInt();

System.out.print("Enter number of columns in second matrix:");

n = s.nextInt();

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 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 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++)

for (int k = 0; k < q; k++)

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");

}}

You might also like