0% found this document useful (0 votes)
4 views4 pages

Java Program for Matrix Addition

The document describes a Java program that adds two matrices of user-defined dimensions. It prompts the user to enter the number of rows and columns, then takes input for the two matrices, computes their sum, and displays the result. The program utilizes a Scanner for input and performs element-wise addition of the matrices.

Uploaded by

sanketh.s577425
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)
4 views4 pages

Java Program for Matrix Addition

The document describes a Java program that adds two matrices of user-defined dimensions. It prompts the user to enter the number of rows and columns, then takes input for the two matrices, computes their sum, and displays the result. The program utilizes a Scanner for input and performs element-wise addition of the matrices.

Uploaded by

sanketh.s577425
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

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

You might also like