Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class MatrixMultiply{
- static int rA, cA, A[][], rB, cB, B[][], C[][];
- static Scanner sc = new Scanner(System.in);
- static void input(){
- int i, j;
- System.out.print("Rows of A: ");
- rA = sc.nextInt();
- System.out.print("Coloumns of A/Rows of B: ");
- cA = sc.nextInt();
- rB = cA;
- System.out.print("Couloumns of B: ");
- cB = sc.nextInt();
- A = new int [rA][cB];
- B = new int [rB][cB];
- C = new int [rA][cB];
- System.out.println("Enter the elements of A:-");
- for(i = 0; i < rA; i++){
- for(j = 0; j < cA; j++)
- A[i][j] = sc.nextInt();
- System.out.println();
- }
- System.out.println("Enter the elements of B:-");
- for(i = 0; i < rB; i++){
- for(j = 0; j < cB; j++)
- B[i][j] = sc.nextInt();
- System.out.println();
- }
- }
- static void multiply(){
- int i, j, k;
- for(i = 0; i < rA; i++){
- for(j = 0; j < cB; j++){
- C[i][j] += 0;
- for(k = 0; k < cA; k++)
- C[i][j] += A[i][k] * B[k][j];
- }
- }
- }
- static void output(){
- int i, j;
- System.out.println("Matrix A is :-");
- for(i = 0; i < rA; i++){
- for(j = 0; j < cA; j++)
- System.out.print(A[i][j]+"\t");
- System.out.println();
- }
- System.out.println("Matrix B is :-");
- for(i = 0; i < rB; i++){
- for(j = 0; j < cB; j++)
- System.out.print(B[i][j]+"\t");
- System.out.println();
- }
- System.out.println("The result is:-");
- for(i = 0; i < rA; i++){
- for(j = 0; j < cB; j++)
- System.out.print(C[i][j]+"\t");
- System.out.println();
- }
- }
- public static void main(String[] args){
- input();
- multiply();
- output();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement