0% found this document useful (0 votes)
8 views1 page

Exp 1

Experiment 1

Uploaded by

lasele3905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Exp 1

Experiment 1

Uploaded by

lasele3905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//Matrix Addition

import java.util.Scanner;

public class MatrixAddition {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// Input matrix dimensions


System.out.print("Enter the number of rows: ");
int rows = input.nextInt();
System.out.print("Enter the number of columns: ");
int columns = input.nextInt();

// Initialize matrices
int[][] matrix1 = new int[rows][columns];
int[][] matrix2 = new int[rows][columns];
int[][] sum = new int[rows][columns];

// Input elements for the first matrix


System.out.println("Enter elements for the first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix1[i][j] = input.nextInt();
}
}

// Input elements for the second matrix


System.out.println("Enter elements for the second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix2[i][j] = input.nextInt();
}
}

// Perform matrix addition


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the result


System.out.println("Sum of the matrices:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}

input.close();
}
}

You might also like