Advertisement
Guest User

TheMatrix

a guest
Feb 25th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TheMatrix {
  4. public static void main(String[] args) {
  5.  
  6. Scanner scan = new Scanner(System.in);
  7. System.out.println("Enter number of rows :");
  8. int rows = scan.nextInt();
  9. System.out.println("Enter number of columns :");
  10. int columns = scan.nextInt();
  11.  
  12. int x = 3;
  13. int y = 3;
  14.  
  15. int[][] firstMatrix = { {2, 3, 4}, {5, 2, 3} };
  16. int[][] secondMatrix = { {-4, 5, 3}, {5, 6, 3} };
  17.  
  18. int[][] addingFirstMatrix = new int[rows][columns];
  19. // Adding Two matrices
  20. for(int i = 0; i < rows; i++) {
  21. for (int j = 0; j < columns; j++) {
  22. System.out.println(" Enter the number of position " + i + "," + j + " in matrix");
  23. addingFirstMatrix[i][j] = scan.nextInt();
  24. }
  25. }
  26. int[][] addingSecondMatrix = new int[rows][columns];
  27. for(int i = 0; i < rows; i++) {
  28. for (int j = 0; j < columns; j++) {
  29. System.out.println(" Enter the number of position " + i + "," + j + " in matrix");
  30. addingSecondMatrix[i][j] = scan.nextInt();
  31. }
  32. }
  33.  
  34. // sum of two matrices
  35. int[][] sum = new int[rows][columns];
  36. for(int i = 0; i < rows; i++) {
  37. for (int j = 0; j < columns; j++) {
  38. sum[i][j] = addingFirstMatrix[i][j] + addingSecondMatrix[i][j];
  39. }
  40. }
  41.  
  42. // sum
  43. System.out.println("Sum of two matrices is: ");
  44. for(int[] row : sum) {
  45. for (int column : row) {
  46. System.out.print(column + " ");
  47. }
  48. System.out.println();
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement