TEXT   17
matrix
Guest on 10th March 2023 10:27:46 AM


  1. import java.util.Scanner;
  2. public class Mat{
  3. public static void main(String[] args)
  4. {
  5. int i,j;
  6. Scanner sc=new Scanner(System.in);
  7. System.out.print("Enter 1st matrix column limit:");
  8. int m=sc.nextInt();
  9. System.out.print("Enter 2nd matrix row limit:");
  10. int n=sc.nextInt();
  11. {
  12. int[][]mat1=new int[m][n];
  13. int[][]mat2=new int[m][n];
  14. int[][]mat3=new int[m][n];
  15. System.out.print("Enter 1st matrix elements:\n");
  16. for(i=0;i<m;i++){
  17.   for(j=0;j<n;j++){
  18.    mat1[i][j]=sc.nextInt();
  19. }
  20. }
  21. System.out.print("Enter 2nd matrix elements:\n");
  22. for(i=0;i<m;i++){
  23.   for(j=0;j<n;j++){
  24.    mat2[i][j]=sc.nextInt();
  25. }
  26. }
  27. for(i=0;i<m;i++){
  28.   for(j=0;j<n;j++){
  29.    mat3[i][j]=mat1[i][j]+mat2[i][j];
  30. }
  31. }
  32. System.out.print("The result matrix:\n");
  33. for(i=0;i<m;i++){
  34.   for(j=0;j<n;j++){
  35.    System.out.print(mat3[i][j]+"\t");
  36. }
  37.    System.out.println();
  38. }
  39. }
  40. }
  41. }

Raw Paste

Login or Register to edit or fork this paste. It's free.