Sunday, February 19, 2017

Write a JAVA program to determine Multiplication of two matrices

import java.util.Scanner;

class MulTwoMatrix
{
   public static void main(String args[])
   {
      int c,d,e;
      Scanner in = new Scanner(System.in);

      int first[][] = new int[3][3];
      int second[][] = new int[3][3];
      int mul[][] = new int[3][3];

      System.out.println("Enter the elements of first matrix");

      for (  c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
            first[c][d] = in.nextInt();

      System.out.println("Enter the elements of second matrix");

      for ( c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
            second[c][d] = in.nextInt();

      for ( c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
for(e=0;e<3;e++)
             mul[c][d] = mul[c][d]+( first[c][e] * second[e][d]);

      System.out.println("Multiplication of entered matrices:-");

      for ( c = 0 ; c < 3 ; c++ )
      {
         for ( d = 0 ; d < 3 ; d++ )
            System.out.print(mul[c][d]+"\t");

         System.out.println();
      }
   }
}

output:
>java MulTwoMatrix
Enter the elements of first matrix
1 2 3
4 5 6
7 8 9
Enter the elements of second matrix
1 2 3
4 5 6
7 8 9
Multiplication of entered matrices:-
30      36      42
66      81      96

102     126     150

No comments:

Post a Comment

Write a JAVA program to sort an array of Strings

import java.util.Scanner; public class SortStrings {     public static void main(String[] args)     {         int n;         String t...