IGNOU - MCSL025 - SECTION04 - [S02_E01]

♠ Posted by Unknown in , at 07:13

IGNOU - MCSL025 - SECTION04 - [S02_E01]

/*Author: Viral Vyas
 *Write a program in java to find A X B where A is matrix of 3X3 and B is a 
 * matrix of 3X4. Take the values in matrixes A and B from the user.
 */
package mcsl025;
import java.io.*;
import java.lang.*;

public class S02E01 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(
System.out.print("\nEnter Value for Matrix-02...\n");
//Product of Two Matrices
System.out.print("\nProduct of Two Matrices...\n");



        
        int m1[][], m2[][], mans[][];
        m1 = new int[3][3];
        m2 = new int[3][4];
        mans = new int[3][4];
        
int i, j, k, sum=0;

                new InputStreamReader(System.in));
        
System.out.print("\nEnter Value for Matrix-01...\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.print("Enter Value of m1[" + i + "][" + j +"] : ");
m1[i][j] = Integer.parseInt(br.readLine());
}
}

for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
System.out.print("Enter Value of m2[" + i + "][" + j +"] : ");
m2[i][j] = Integer.parseInt(br.readLine());
}
}

for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
sum = 0;
for(k=0; k<3; k++)
{
sum = sum + (m1[i][k] * m2[k][j]);
}
mans[i][j] = sum;
}
}

for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
System.out.print(mans[i][j] + " ");
}
System.out.print("\n");
}
    }
}

Output:

Enter Value for Matrix-01...
Enter Value of m1[0][0] : 1
Enter Value of m1[0][1] : 2
Enter Value of m1[0][2] : 3
Enter Value of m1[1][0] : 4
Enter Value of m1[1][1] : 5
Enter Value of m1[1][2] : 6
Enter Value of m1[2][0] : 7
Enter Value of m1[2][1] : 8
Enter Value of m1[2][2] : 9

Enter Value for Matrix-02...
Enter Value of m2[0][0] : 1
Enter Value of m2[0][1] : 2
Enter Value of m2[0][2] : 3
Enter Value of m2[0][3] : 4
Enter Value of m2[1][0] : 5
Enter Value of m2[1][1] : 6
Enter Value of m2[1][2] : 7
Enter Value of m2[1][3] : 8
Enter Value of m2[2][0] : 9
Enter Value of m2[2][1] : 10
Enter Value of m2[2][2] : 11
Enter Value of m2[2][3] : 12

Product of Two Matrices...
38   44   50    56 
83   98   113  128 
128 152 176  200

0 comments:

Post a Comment