Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Matrix multiplication
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Supriya Kurian
Matrix multiplication
3 Answer(s)      4 years and a month ago
Posted in : Java Beginners


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


Advertisement
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 ulaganathan.n@laserwords.co.in");
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
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
Java, matrix
"); System.out.println("3. Multiplication Of Matrix"); System.out.println("4. Exit"); boolean quit... of switch statement perform operations addition, subtraction and multiplication of matrix.   Hello Friend, Try this: import java.util.*; class
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
matrix addition
matrix addition  hai
matrix addition
matrix addition  hai
matrix substraction
matrix substraction  hai
matrix substraction
matrix substraction  hai
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
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
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
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
matrix determination in java
matrix determination in java  hai
matrix polynamial addtion
matrix polynamial addtion  hai
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
Magic Matrix in GUI
Magic Matrix in GUI  I want program in java GUI contain magic matrix for numbers
Advertisements
 

 

 

DMCA.com