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

Explain Java Command Line Arguments?

The way of providing input at the time of exection is known as command line arguments. Command line arguments are parameters that are supplied to the application program at...

Explain Java Tokens?

A Java program is basically a collection of classes. A class is defined by a set of declaration statements and methods containing executable statements. ...

Explain How Java Is Strongly Associated With Internet And WWW?

Java is strongly associated with the internet because of the first application program written in Java was hot Java. Web browsers to run applets on internet. ...

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