java program to get output on the basis of users choice of switch statement perform operations addition, subtraction and multiplication of matrix.
import java.io.*; class Matrix { int a ,b,i,j; int matrix[][]=new int [12][14]; void Read() { int a ,b,i,j; int matrix[][]=new int [12][14]; DataInputStream is=new DataInputStream(System.in); try { System.out.println("enter the row"); a=Integer.parseInt(is.readLine()); System.out.println("enter the col;umn"); b=Integer.parseInt(is.readLine()); System.out.println("enter the elements for the matrix"); for(i=0;i<a;i++) { for(j=0;j<b;j++) { matrix[i][j]=Integer.parseInt(is.readLine()); } } System.out.println("the matrix that you entered is"); for(i=0;i<a;i++) { for(j=0;j<b;j++) { System.out.print(matrix[i][j]); } } } catch(Exception ex) {} } public static void main(String args[]) { Matrix m=new Matrix(); m.Read(); } }
Hello Friend,
Try this:
import java.util.*; class MatrixExamples{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int[][] A = new int[2][2]; int[][] B = new int[2][2]; int[][] C = new int[2][2]; System.out.println("Enter elements for matrix A : "); for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = input.nextInt(); } System.out.println("Enter elements for matrix B : "); for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = input.nextInt(); } System.out.println("Matrix A: "); for (int i=0 ; i < A.length ; i++) { System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println("Matrix B: "); for (int i=0 ; i < B.length ; i++) { System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); int menu = 0; System.out.println(); System.out.println("1. Addition Of Matrix"); System.out.println("2. Subtraction Of Matrix"); System.out.println("3. Multiplication Of Matrix"); System.out.println("4. Exit"); boolean quit = false; do{ System.out.print("Please enter your choice: "); menu = input.nextInt(); System.out.println(); switch(menu) { case 1: System.out.println("*Sum of 2 matrices*"); for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ C[i][j]=A[i][j]+B[i][j]; System.out.print(C[i][j]+" "); } System.out.println(); } break; case 2: System.out.println("Subtraction of 2 matrices*"); for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ C[i][j]=A[i][j]-B[i][j]; System.out.print(C[i][j]+" "); } System.out.println(); } break; case 3: System.out.println("*Multiplication of 2 matrices*"); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { for(int k=0;k<2;k++) { C[i][j]+=A[i][k]*B[k][j]; } } } for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { System.out.print(+C[i][j]+" "); } System.out.println(); } break; case 4: quit = true; break; default: System.out.println("Invalid Entry!"); } } while (!quit); } }
Thanks
import java.io.*; class Matrix { int a ,b,i,j; int matrix[][]=new int [12][14]; void Read() { int a ,b,i,j; int matrix[][]=new int [12][14]; DataInputStream is=new DataInputStream(System.in); try { System.out.println("enter the row"); a=Integer.parseInt(is.readLine()); System.out.println("enter the col;umn"); b=Integer.parseInt(is.readLine()); System.out.println("enter the elements for the matrix"); for(i=0;i<a;i++) { for(j=0;j<b;j++) { matrix[i][j]=Integer.parseInt(is.readLine()); } } System.out.println("the matrix that you entered is"); for(i=0;i<a;i++) { for(j=0;j<b;j++) { System.out.print(matrix[i][j]); } } } catch(Exception ex) {} } public static void main(String args[]) { Matrix m=new Matrix(); m.Read(); } }
import java.io.*; class Matrix { int a ,b,i,j; int matrix[][]=new int [12][14]; void Read() { int a ,b,i,j; int matrix[][]=new int [12][14]; DataInputStream is=new DataInputStream(System.in); try { System.out.println("enter the row"); a=Integer.parseInt(is.readLine()); System.out.println("enter the col;umn"); b=Integer.parseInt(is.readLine()); System.out.println("enter the elements for the matrix"); for(i=0;i<a;i++) { for(j=0;j<b;j++) { matrix[i][j]=Integer.parseInt(is.readLine()); } } System.out.println("the matrix that you entered is"); for(i=0;i<a;i++) { for(j=0;j<b;j++) { System.out.print(matrix[i][j]); } } } catch(Exception ex) {} } public static void main(String args[]) { Matrix m=new Matrix(); m.Read(); } }
Ads