Moyee Moyee

mail@pastecode.io avatar
unknown
java
2 months ago
1.8 kB
3
Indexable
Never
// Importing required packages
import java.util.Scanner;

// Driver Code
public class lab2{
	public static void main(String args[]){

		// Creating a Scanner class object to take user input
		Scanner scanner = new Scanner(System.in);

		// Taking the size of the matrix
		System.out.print("Enter row size : ");
		int row = scanner.nextInt();
		System.out.print("Enter col size : ");
		int col = scanner.nextInt();
		
		// Initializing the matrix
		int matrix1[][] = new int[row][col];
		int matrix2[][] = new int[row][col];

		// Taking elements of Matrix 1
		System.out.println("\n\nMatrix 1");
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				System.out.print("Enter the value in ["+i+"]["+j+"] : ");
				matrix1[i][j] = scanner.nextInt();
			}
		}

		// Displaying Matrix 1
		System.out.println("");
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				System.out.print(matrix1[i][j]+" ");
			}
			System.out.println("");
		}
		System.out.println("");
		System.out.println("");

		// Taking elements of Matrix 2
		System.out.println("Matrix 2");
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				System.out.print("Enter the value in ["+i+"]["+j+"] : ");
				matrix2[i][j] = scanner.nextInt();
			}
		}

		// Displaying Matrix 2
		System.out.println("");
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				System.out.print(matrix2[i][j]+" ");
			}
			System.out.println("");
		}

		// Displaying sum of Matrix 1 and 2
		System.out.println("");
		System.out.println("");
		System.out.println("Matrix 1 + Matrix 2 :");
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				System.out.print((matrix1[i][j]+matrix2[i][j])+" ");
			}
			System.out.println("");
		}

		// Closing the object to free up the memory
		scanner.close();
	}
}
Leave a Comment