Write a program in Java to multiply two matrix.

 

AIM:   Write a program in Java to multiply two matrix.

 

CODE:

import java.util.Scanner;

public class p10

{

    public static void main(String args[])

    {

        Scanner readme = new Scanner(System.in);

        int[][] n1 = new int[3][3];

        int[][] n2 = new int[3][3];

        int[][] ans = new int[3][3];

        System.out.println("Enter the elements for 1st matrix \n");

        for (int i = 0; i < 3; i++)

        {

            for (int j = 0; j < 3; j++)

            {

                n1[i][j] = readme.nextInt();

            }

        }

        System.out.println("Enter the elements for 2nd matrix\n");

        for (int i = 0; i < 3; i++)

        {

            for (int j = 0; j < 3; j++)

            {

                n2[i][j] = readme.nextInt();

            }

        }

        for (int i = 0; i < 3; i++)

        {

            for (int j = 0; j < 3; j++)

            {

                ans[i][j]=0;

                for (int k = 0; k < 3; k++)

                {

                    ans[i][j]+=n1[i][k]*n2[k][j];

                }

            }

        }

        System.out.println("Multiplication:\n");

        for (int i = 0; i < 3; i++)

        {

            for (int j = 0; j < 3; j++)

            {

                System.out.print(ans[i][j] + " ");

            }

            System.out.println("\n");

        }

    }

}

 

OUTPUT:



Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program that illustrates interface inheritance. Interface P12 inherits from both P1 and P2. Each interface declares one constant and one method. The class Q implements P12. Instantiate Q and invoke each of its methods. Each method displays one of the constants.