2D Array Calculator (or 4func. Matrix Calculator)
- Sep 5, 2017
- 2 min read
source code:
import java.util.Scanner; class array2dCalc{ public static void main(String args[]) { int arr1[][]= new int[3][3]; int arr2[][]= new int[3][3]; int arr3[][]= new int[3][3]; int i,j,ch,sum=0; Scanner sc = new Scanner(System.in); do { System.out.println("\n=============Matrix Calculator==========\n"); System.out.println("Choose an Operation: \n"); System.out.println("1. Addition:\n"+ "2. Subtraction:\n"+ "3. Multiplication:\n"+ "4. Transpose:\n"+ "5. Exit:\n"); ch= sc.nextInt(); switch(ch) { case 1: System.out.println("\n=======Matrix Sum=======\n"); System.out.println("Enter values in first 3 X 3 Matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr1[i][j] = sc.nextInt(); } } System.out.println("Enter values in second 3 X 3 Matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr2[i][j] = sc.nextInt(); } } System.out.println("\nSum of the Matrices is: \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr3[i][j] = arr1[i][j]+arr2[j][j]; System.out.print(arr3[i][j]+" "); } System.out.println(); } break; case 2: System.out.println("\n=======Matrix Difference=======\n"); System.out.println("Enter values in first 3 X 3 Matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr1[i][j] = sc.nextInt(); } } System.out.println("Enter values in second 3 X 3 Matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr2[i][j] = sc.nextInt(); } } System.out.println("\nDifference of the Matrices is: \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr3[i][j] = arr1[i][j]-arr2[j][j]; System.out.print(arr3[i][j]+" "); } System.out.println(); } break; case 3: System.out.println("\n=======Matrix Product=======\n"); System.out.println("Enter values in first 3 X 3 Matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr1[i][j] = sc.nextInt(); } } System.out.println("Enter values in second 3 X 3 Matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr2[i][j] = sc.nextInt(); } } System.out.println("\nProduct of the Matrices is: \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { for(int k=0; k<3; k++) { sum = sum+arr1[i][k]*arr2[k][j]; } arr3[i][j]= sum ; sum = 0; } } for(i=0; i<3; i++) { for(j=0; j<3; j++) { System.out.print(arr3[i][j]+" "); } System.out.println(); } break; case 4: System.out.println("\n=======Transpose Of Matrix=======\n"); System.out.println("Enter number of rows: "); int row = sc.nextInt(); System.out.println("Enter number of columns: "); int col = sc.nextInt(); int array[][]= new int[row][col]; System.out.println("Enter "+row+" X "+col+" Matrix: "); for(i=0; i<row; i++) { for(j=0; j<col; j++) { array[i][j] = sc.nextInt(); } } System.out.println("Matrix Before Transpose: "); for(i=0; i<row; i++) { for(j=0; j<col; j++) { System.out.print(array[i][j]+" "); } System.out.println(); } System.out.println("\nTranspose of Matrix is: \n"); for(i=0; i<col; i++) { for(j=0; j<row; j++) { System.out.print(array[j][i]+" "); } System.out.println(); } break; case 5: System.exit(0); break; default: System.out.println("plz enter a valid input!"); } }while(ch != 5); } }
Output:











Comments