Commit d043845c authored by Qazi Zain's avatar Qazi Zain

new task are added

parent e832acbb
package JavaTrainingTask.Question9;
import java.util.Scanner;
public class MatrixMultiplication {
MatrixMultiplication() {
System.out.println("object is created");
}
public static void inputMatrix(int[][] matrix, int rows, int cols, Scanner input, String matrixName)
{
System.out.println("Enter the elements of the " + matrixName + " matrix:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[i][j] = input.nextInt();
}
}
}
public static void multiplyMatrices(int[][] matrixA, int[][] matrixB, int[][] matrixC, int m, int n, int p)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < p; j++)
{
matrixC[i][j] = 0;
for (int k = 0; k < n; k++)
{
matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
}
public static void displayMatrix(int[][] matrix, int rows, int cols)
{
System.out.println("The product of the matrices is:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
package JavaTrainingTask.Question9;
import java.util.Scanner;
public class main {
public static void main(String[] args)
{
MatrixMultiplication obj = new MatrixMultiplication();
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows of the first matrix:");
int m = input.nextInt();
System.out.println("Enter the number of columns of the first matrix (and rows of the second):");
int n = input.nextInt();
System.out.println("Enter the number of columns of the second matrix:");
int p = input.nextInt();
int[][] matrixA = new int[m][n];
int[][] matrixB = new int[n][p];
int[][] matrixC = new int[m][p];
obj.inputMatrix(matrixA, m, n, input, "first");
obj.inputMatrix(matrixB, n, p, input, "second");
obj.multiplyMatrices(matrixA, matrixB, matrixC, m, n, p);
obj.displayMatrix(matrixC, m, p);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment