JAVA PROGRAM FOR MATRIX MULTIPLICATION

import java.io.*; class Matrix {    public static void main(String args[])throws IOException     {             BufferedReader br...

import java.io.*;
class Matrix
{
   public static void main(String args[])throws IOException
    {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter order of 1st matrix");
            int m=Integer.parseInt(br.readLine());
            int n=Integer.parseInt(br.readLine());
            int a[][]=new int[m][n];         
            System.out.println("enter elements into 1st matrix");
            for(int i=0; i < m; i++ )
            {
                        for(int j=0; j < n; j++ )
                        {
                                    a[i][j]=Integer.parseInt(br.readLine());
                        }
            }
            System.out.println("first matrix is");
            for(int i=0; i < m; i++ )
            {
                        System.out.print("||");
                        for(int j=0; j < n; j++ )
                        {          
                                    System.out.print(" "+a[i][j]);                        
                                   
                        }
                        System.out.print("||\n");
            }
            System.out.println("enter order of 2nd matrix");
            int p=Integer.parseInt(br.readLine());
            int q=Integer.parseInt(br.readLine());
            int b[][]=new int[p][q];
            System.out.println("enter elements into 2nd matrix");
            for(int i=0; i < p; i++ )
            {
                        for(int j=0; j < q; j++ )
                        {
                                    b[i][j]=Integer.parseInt(br.readLine());
                        }
            }
           
            System.out.println("second matrix is");
            for(int i=0; i < p; i++ )
            {
                        System.out.print("||");
                        for(int j=0; j < q; j++ )
                        {          
                                    System.out.print(" "+b[i][j]);                        
                                   
                        }
                        System.out.print("||\n");
            }                      
            if(n==p)
            {
                        System.out.println("maltiplication possible");
                        int c[][]=new int[m][q];
                        for(int i=0; i < m; i++ )
                        {
                                    for(int j=0; j < q; j++ )
                                    {
                                                c[i][j]=0;
                                                for(int k=0; k < n; k++ )
                                                {
                                                            c[i][j]=c[i][j]+a[i][k]*b[k][j];
                                                }
                                    }
                        }
                        System.out.println("resultent matrix is");
                        for(int i=0; i
                        {
                                    System.out.print("||");
                                    for(int j=0; j < n ; j++ )
                                    {          
                                                System.out.print(" "+c[i][j]);
                                    }
                                    System.out.print("||\n");
                        }          
            }
            else
            {
                        System.out.println("MULTIPLICATION NOT POSSIBLE");
            }
}
}

 OUTPUT:

enter order of 1st matrix
2
2
enter elements into 1st matrix
1
2
3
4

first matrix is
|| 1   2||
|| 3   4||

enter order of 2nd matrix
2
2

enter elements into 2nd matrix
5
6
7
8

second matrix is
|| 5   6||
|| 7   8||

maltiplication possible
resultent matrix is

|| 19   22||

|| 43   50||

Related

How Many Ways can you Create a Threads in Java?

Threads  are  implemented  in  the  form of  objects  that contain method  called  run(). The run() method is the heart and soul of any thread. It make u...

Write about SYNCHRONIZATION in Java?

SYNCHRONIZATION: Threads that use their own data and methods provided  inside their run( ) methods. What happens when they try to use data and methods outside themselves  on such occa...

Explain about Various Types of Operators in Java?

Java has rich set of operators. ® Operators are of any symbol that tells the computer to perform certain mathematical (Or) logical manipulations. ® Operat...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item