Matrix Manipulation Using Java

Introduction

In this blog, I will explain about matrix manipulation in Java. It is very simple in Java Programming. The output will be displayed in the Run module.

Software Requirement

JDK1.3.

Matrix Manipulation

In this program, I explain about matrix addition, subtraction, and multiplication.

Simple program

  1. class mat  
  2.  {  
  3.    public static void main(String arg[])  
  4.     {  
  5.       int a[][] = {{1,2},{3,4}};  
  6.       int b[][] = {{1,2},{3,4}};  
  7.   
  8.       int c[][] = new int[2][2];  
  9.   
  10.       int i,j,k;  
  11.      
  12.        System.out.println("\nGiven A Matrix is...");   
  13.        for(i=0;i<2;i++)  
  14.         {  
  15.          for(j=0;j<2;j++)  
  16.           {  
  17.             System.out.print(a[i][j]+"\t");  
  18.           }  
  19.             System.out.println("\n");             
  20.         }  
  21.   
  22.        System.out.println("\nGiven B Matrix is...");   
  23.        for(i=0;i<2;i++)  
  24.         {  
  25.          for(j=0;j<2;j++)  
  26.           {  
  27.             System.out.print(b[i][j]+"\t");  
  28.           }  
  29.             System.out.println("\n");             
  30.         }  
  31.   
  32.        
  33.        for(i=0;i<2;i++)  
  34.         {  
  35.          for(j=0;j<2;j++)  
  36.           {  
  37.             c[i][j] = a[i][j] + b[i][j];  
  38.           }  
  39.          }  
  40.   
  41.        System.out.println("\nMatrix Addition is...");   
  42.        for(i=0;i<2;i++)  
  43.         {  
  44.          for(j=0;j<2;j++)  
  45.           {  
  46.             System.out.print(c[i][j]+"\t");  
  47.           }  
  48.             System.out.println("\n");             
  49.         }  
  50.   
  51.        for(i=0;i<2;i++)  
  52.         {  
  53.          for(j=0;j<2;j++)  
  54.           {  
  55.             c[i][j] = a[i][j] - b[i][j];  
  56.           }  
  57.          }  
  58.   
  59.        System.out.println("\nMatrix Subtraction is...");   
  60.        for(i=0;i<2;i++)  
  61.         {  
  62.          for(j=0;j<2;j++)  
  63.           {  
  64.             System.out.print(c[i][j]+"\t");  
  65.           }  
  66.             System.out.println("\n");             
  67.         }  
  68.   
  69.        for(i=0;i<2;i++)  
  70.         {  
  71.          for(j=0;j<2;j++)  
  72.           {  
  73.             for(k=0;k<2;k++)  
  74.              {  
  75.                 c[i][j] = c[i][j] + a[i][k] * b[k][j];  
  76.              }  
  77.           }  
  78.          }  
  79.   
  80.        System.out.println("\nMatrix Multiplication is...");   
  81.               for(i=0;i<2;i++)  
  82.         {  
  83.          for(j=0;j<2;j++)  
  84.           {  
  85.             System.out.print(c[i][j]+"\t");  
  86.           }  
  87.             System.out.println("\n");             
  88.         }          
  89.   
  90.      }  
  91.   }  
  92.     
 Explanation


In this blog, I will explain about matrix manipulation in Java programming. The output will be displayed in the Run module.

Output

Ebook Download
View all
Learn
View all