Tuesday, March 8, 2011

Matrix Addition, Subtraction and Multiplication in Java

This a simple java code which add, subtract and multiply matrices. In running this program just enter the row and column size, and then you will input the element of the first and second matrix based on the row and column size you created. Then it will print out the added, subtracted and multiplied matrix.

My problem in this program is how to repeat it all over again. You can see at the last part of this program I made this statement "Play again? [Y/N]". If the user input "y" or "Y", it should run the program again otherwise the program will exit. And my problem is it won't run again if i select "y" or "Y", but there's no problem in compiling.

If anybody here can help me..show me some codes please..thanks.


import java.io.*;
import java.util.*;
public class matrixadd{
   
public static void main(String[]args)throws IOException{
   
    Scanner scan = new Scanner(System.in);
   
    int mat1[][]=new int [10][10];
    int mat2[][]=new int [10][10];
    int mat3[][]=new int [10][10];
   
    System.out.print("Number of row: ");
    int row = scan.nextInt();
    System.out.print("Number of column: ");
    int col = scan.nextInt();
    System.out.println();
   
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        System.out.print("Enter element "+(i+1)+":"+(j+1)+" of first table = ");
        mat1[i][j] = scan.nextInt();
        } }
   
    System.out.println("\nTable 1:");
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        System.out.print(mat1[i][j]+" ");
    }System.out.println();
    }
   
    System.out.println();
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        System.out.print("Enter element "+(i+1)+":"+(j+1)+" of second table = ");
        mat2[i][j] = scan.nextInt();
    }}
   
    System.out.println("\nTable 2:");
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        System.out.print(mat2[i][j]+" ");
    }System.out.println();
    }
   
    System.out.println("\nAdded Matrix: ");
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        mat3[i][j] = mat1[i][j] + mat2[i][j];
        System.out.print(mat3[i][j]+" ");
    }System.out.println();
    }
   
        System.out.println("\nSubtracted Matrix: ");
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        mat3[i][j] = mat1[i][j] - mat2[i][j];
        System.out.print(mat3[i][j]+" ");
    }System.out.println();
    }
   
    int mat4[][]=new int[10][10];
    for(int i=0;i<row;i++){
    for(int j=0;j<col;j++){
    for(int k=0;k<col;k++){
        mat4[i][j]+=mat1[i][k]*mat2[k][j];
    }}}

    System.out.println("\nMultiplied Matrix:");
    for(int i=0;i<row;i++){
    for(int j=0;j<col;j++){
        System.out.print(mat4[i][j]+" ");
    }System.out.println();
    }

BufferedReader geoff = new BufferedReader(new InputStreamReader(System.in));
   
System.out.println("\nPlay again?[Y/N]\n");
String answer = geoff.readLine();
if(answer == "Y" || answer == "y"){
System matrixadd;
} else {
System.exit(0);
} }}

5 comments: