6 B. Write a java program to add two matrices and print the resultant matrix.

 Program :

import java.util.Scanner;

public class MatAdd

{

public static void main(String args[])

{

int i, j;

int mat1[][] = new int[2][2];

int mat2[][] = new int[2][2];

int mat3[][] = new int[2][2];

Scanner sc = new Scanner(System.in);

System.out.print("Enter Matrix 1 Elements : ");

for(i=0; i<2; i++)

{

for(j=0; j<2; j++)

{

mat1[i][j] = sc.nextInt();

}

}

System.out.print("Enter Matrix 2 Elements : ");

for(i=0; i<2; i++)

{

for(j=0; j<2; j++)

{

mat2[i][j] = sc.nextInt();

}

}

System.out.print("Adding both Matrix to form the Third Matrix...\n");


System.out.print("The Two Matrix Added Successfully..!!\n");

System.out.print("The New Matrix will be :\n");


for(i=0; i<2; i++)

{

for(j=0; j<2; j++)

{

mat3[i][j] = mat1[i][j] + mat2[i][j];

System.out.print(mat3[i][j]+ " ");

}

System.out.println("");

}

}

}


Comments

Popular posts from this blog

Create an android app that demonstrates Activity Lifecycle and Instance State.

Program using Light Sensitive Sensors on Tinkercad