Matrix multiplication

Matrix multiplication

program to read the elements of the given two matrices of order n*n and to perform the matrix multiplication.

View Answers

December 10, 2010 at 1:11 PM

Hi Friend,

Try the following code:

import java.util.*;
class MatrixMultiplication{

    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    int[][] A = new int[3][3];
int[][] B = new int[3][3];
int[][] C = new int[3][3];
System.out.println("Enter elements for matrix A : ");
for (int i=0 ; i < A.length ; i++)
for  (int j=0 ; j < A[i].length ; j++){
A[i][j] = input.nextInt();
}
System.out.println("Enter elements for matrix B : ");
for (int i=0 ; i < B.length ; i++)
for  (int j=0 ; j < B[i].length ; j++){
B[i][j] = input.nextInt();
}
System.out.println("Matrix A: ");
        for (int i=0 ; i < A.length ; i++){
            System.out.println();
            for  (int j=0 ; j < A[i].length ; j++){
                System.out.print(A[i][j]+" ");
                  }
        }
System.out.println();
System.out.println();
System.out.println("Matrix B: ");
        for (int i=0 ; i < B.length ; i++){    
            System.out.println();
            for  (int j=0 ; j < B[i].length ; j++){
                System.out.print(B[i][j]+" ");
                  }
        }
System.out.println();
System.out.println();
System.out.println("Result is: ");
System.out.println();

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
C[i][j]+=A[i][k]*B[k][j];
}
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(+C[i][j]+" ");
}
System.out.println();
} 
}
}

Thanks


December 10, 2010 at 1:12 PM

Hi Friend,

Try the following code:

import java.util.*;
class MatrixMultiplication{

    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    int[][] A = new int[3][3];
int[][] B = new int[3][3];
int[][] C = new int[3][3];
System.out.println("Enter elements for matrix A : ");
for (int i=0 ; i < A.length ; i++)
for  (int j=0 ; j < A[i].length ; j++){
A[i][j] = input.nextInt();
}
System.out.println("Enter elements for matrix B : ");
for (int i=0 ; i < B.length ; i++)
for  (int j=0 ; j < B[i].length ; j++){
B[i][j] = input.nextInt();
}
System.out.println("Matrix A: ");
        for (int i=0 ; i < A.length ; i++){
            System.out.println();
            for  (int j=0 ; j < A[i].length ; j++){
                System.out.print(A[i][j]+" ");
                  }
        }
System.out.println();
System.out.println();
System.out.println("Matrix B: ");
        for (int i=0 ; i < B.length ; i++){    
            System.out.println();
            for  (int j=0 ; j < B[i].length ; j++){
                System.out.print(B[i][j]+" ");
                  }
        }
System.out.println();
System.out.println();
System.out.println("Result is: ");
System.out.println();

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
C[i][j]+=A[i][k]*B[k][j];
}
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(+C[i][j]+" ");
}
System.out.println();
} 
}
}

Thanks


July 7, 2011 at 10:11 PM

import java.util.*;
import java.io.*;
import java.math.*;
class u1
{
 public static void main(String[] args)throws IOException
  {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Square matrix size");
int u=Integer.parseInt(br.readLine());
int f=Integer.parseInt(br.readLine());
int u1[][]=new int[u][f];
int f1[][]=new int[u][f];
int uf[][]=new int[u][f];
System.out.println("Enter 1'st matrix values");
 for(int i=0;i<u;i++)
  {
   for(int j=0;j<f;j++)
   {
    u1[i][j]=Integer.parseInt(br.readLine());
   }
  }
System.out.println("Enter 2'nd matrix values");
 for(int i=0;i<u;i++)
  {
   for(int j=0;j<f;j++)
   {
    f1[i][j]=Integer.parseInt(br.readLine());
   }
  }
System.out.println("Matrix A is:");
 for(int i=0;i<u;i++)
  {
   for(int j=0;j<f;j++)
   {
    System.out.print("  "+u1[i][j]);
   }
  System.out.println("");
  System.out.println("");
  }
System.out.println("");
System.out.println("Matrix B is:");
 for(int i=0;i<u;i++)
  {
   for(int j=0;j<f;j++)
   {
    System.out.print("  "+f1[i][j]);
   }
  System.out.println("");
  System.out.println("");
  }
System.out.println("");
System.out.println("Done by [email protected]");
System.out.println("Multiplication of Matrix is:");
 for(int i=0;i<u;i++)
  {
   for(int j=0;j<f;j++)
   {
   for(int k=0;k<f;k++)
   {
    uf[i][j]+=u1[i][k]*f1[k][j];
   }
   }
  }

 for(int i=0;i<u;i++)
  {
   for(int j=0;j<f;j++)
   {
    System.out.print("  "+uf[i][j]);
   }
  System.out.println("");
  System.out.println("");
 }
}
}









Related Tutorials/Questions & Answers:
Matrix multiplication
Matrix multiplication  program to read the elements of the given two matrices of order n*n and to perform the matrix multiplication
Parallel multiplication matrix in java
Parallel multiplication matrix in java  hello dear I need parallel multiply matrix in java algorithm to account speed up and efficiency great wishes
Advertisements
parallel dense matrix multiplication
using dense parallel matrix multiplication. I request you to kindly provide me a code for Parallel Matrix multiplication on distributed systems using Java...parallel dense matrix multiplication  hi friends, i am a final year
code for multiplication of matrix in java using methods
code for multiplication of matrix in java using methods  code for multiplication of matrix in java using methods
Matrix Multiplication in Java
Matrix Multiplication in Java In this Java tutorial we will demonstrate you 'Matrix Multiplication in Java' with the help of a simple example from which you can easily learn how to write a matrix multiplication program in Java
Matrix multiplication in java
Matrix multiplication in java In this section we will learn about multiplication of two matrices. In java this is a simple program to multiply two matrices... for the second matrix q = in.nextInt(); // q holding
Multiplication of two Matrix
Multiplication of Two Matrix   ... that teaches you the method to multiply two matrix. We are going to make a simple program that will multiply two matrix. Now to make this program run, you need
Multiplication of two Matrix
Multiplication of two Matrix    ... for multiplying two matrix to each other. Here providing you Java source code... program that will multiply two matrix. Two dimensional array represents
multiplication
multiplication  multiplication of two number numbers entered by user minimum 100digits using GUI   Java Multiplication of two 100 digits number import java.awt.*; import java.math.*; import javax.swing.*; import
multiplication
multiplication  multiplication of two number numbers entered by user minimum 100digits using GUI   Java Multiplication of two 100 digits number import java.awt.*; import java.math.*; import javax.swing.*; import
Multiplication table
Design and develop Multiplication table in Net beans  Design and develop a program that will print a multiplication table. The program should: Ask the user for ending number than it must Display that particular multiplication
Java Matrix Multiplication Example
Java Matrix Multiplication Example In this tutorial, you will learn how... the multiplication of two matrices of any order. In the given program, firstly we have... and then accept the matrix elements as array elements. We have declared two
Multiplication table
Multiplication table Net Beans  Using Net beans Design and develop... for ending number than Display that particular multiplication table   import java.util.Scanner; class Multiplication { public static void main
Multiplication table
Multiplication table  Using Net beans Design and develop a program... number than Display that particular multiplication table   import java.util.Scanner; class Multiplication { public static void main(String args
Java, matrix
"); System.out.println("3. Multiplication Of Matrix"); System.out.println("4. Exit... of switch statement perform operations addition, subtraction and multiplication of matrix.   Hello Friend, Try this:ADS_TO_REPLACE_1 import
matrix addition
matrix addition  hai
matrix addition
matrix addition  hai
matrix substraction
matrix substraction  hai
matrix substraction
matrix substraction  hai
matrix adddition
matrix adddition   how to write a program matrix addition in java
column matrix
column matrix  columan wise total matrix
column matrix
column matrix  columan wise total matrix
ModuleNotFoundError: No module named 'multiplication'
ModuleNotFoundError: No module named 'multiplication'  Hi, My... named 'multiplication' How to remove the ModuleNotFoundError: No module named 'multiplication' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'multiplication'
ModuleNotFoundError: No module named 'multiplication'  Hi, My... named 'multiplication' How to remove the ModuleNotFoundError: No module named 'multiplication' error? Thanks   Hi, In your python
Multiplication problem - Java Beginners
Multiplication problem  I am facing a peculiar problem in java regarding a multiplication. Please see below: 19300 * 0.001 = 19.3 19400 * 0.001 = 19.400000000000002 (why is this ??) 19500 * 0.001 = 19.5 Can anybody help
transpose matrix
integers into the matrix and print the transpose of it. for this program u r given answer but if i entered 2 by 3 matrix it will not give answer ple check it once
Matrix Class
Matrix Class   A class to manage matrices and add them. Create in the driver class two objects of it and use the add method
Matrix Class
Matrix Class   A class to manage matrices and add them. Create in the driver class two objects of it and use the add method
transpose of matrix
transpose of matrix  write a program in java to declare a square matrices 'A' or order n which is less than 20.allow in user to input only positive integers into the matrix and print the transpose
TensorFlow placeholder matrix
matrix with TensorFlow? I am searching for TensorFlow placeholder matrix... matrix and it can easily perform various operations on the matrix. In the following example we are defining a multi-dimensional matrix as placeholder
matrix determination in java
matrix determination in java  hai
matrix polynamial addtion
matrix polynamial addtion  hai
multiplication algorithms in java divide and conquer
multiplication algorithms in java divide and conquer  I need multiplication algorithms in java divide and conquer ask from user input two numbers in binary then the program multiply two number use multiplication algorithm in java
multiplication algorithms in java divide and conquer
multiplication algorithms in java divide and conquer  I need multiplication algorithms in java divide and conquer ask from user input two numbers in binary then the program multiply two number use multiplication algorithm in java
multiplication algorithms in java divide and conquer
multiplication algorithms in java divide and conquer  I need multiplication algorithms in java divide and conquer ask from user input two numbers in binary then the program multiply two number use multiplication algorithm in java
java program_big digits multiplication..
java program_big digits multiplication..  i want program about big digits multiplication program using java..plz tel me answer
java string multiplication
java string multiplication  Is there any simple method of string multiplication in java ..?   public class StringMultiplication { public static void main(String[] args) { String number1 = "17"; String number2
matrix
Magic Matrix in GUI
Magic Matrix in GUI  I want program in java GUI contain magic matrix for numbers
Preprocessor directive case for multiplication and addition
Preprocessor directive case for multiplication and addition  Preprocessor directive case for multiplication and addition include<stdio.h> define m 2+10 include<conio.h> int main() { int i; clrscr
ModuleNotFoundError: No module named 'matrix'
ModuleNotFoundError: No module named 'matrix'  Hi, My Python... 'matrix' How to remove the ModuleNotFoundError: No module named 'matrix'... to install padas library. You can install matrix python with following command
ModuleNotFoundError: No module named 'the-matrix'
ModuleNotFoundError: No module named 'the-matrix'  Hi, My Python... 'the-matrix' How to remove the ModuleNotFoundError: No module named 'the-matrix' error? Thanks   Hi, In your python environment you
print the multiplication table up to one
print the multiplication table up to one  My problem is that I have... Multiplication { public static void main(String args[]) { int n, c; System.out.println("Enter an integer to print it's multiplication table
Preprocessor directive case for multiplication and addition
Preprocessor directive case for multiplication and addition  #include<stdio.h> #define m 2+10 #include<conio.h> int main() { int i; clrscr(); i=m*m; printf("i=%d\n",i); return 0
Preprocessor directive case for multiplication and addition
Preprocessor directive case for multiplication and addition  #include<stdio.h> #define m 2+10 #include<conio.h> int main() { int i; clrscr(); i=m*m; printf("i=%d\n",i); return 0
matrix calculator - Java Beginners
matrix calculator  hi..... can you help me in writing source code of matrix calculator in java... i know you are the best you can do it!!! show yourself
nsstring get value - algebra multiplication problems
nsstring get value - algebra multiplication problems  NSString Algebra multiplication problems How can i get value of X using algebra multiplication problems in nsstring? if i have two values.. 3 + 8 + X
Matrix Addition using RMI in Java
Matrix Addition using RMI in Java  Source Code for Matrix Addition using RMI in Java
Mysql Multiplication Columns
Mysql Multiplication Columns       Mysql Multiplication Columns is used to provide... an example from 'Mysql Multiplication Columns'.To understand the example we
Mysql Multiplication Columns
Mysql Multiplication Columns       Mysql Multiplication Columns is used to provide the column... 'Mysql Multiplication Columns'. To understand the example we simply create

Ads