Exercisel6 Com by HQ
Exercisel6 Com by HQ
Source code
import java.util.Scanner;
scanner.close();
}
}
Output
Exercise 2:
Write a Java program to multiply two matrices of 3x3. Suggest the data
Source Code
public class MatrixMultiplication {
public static void main(String[] args) {
// Sample data for two matrices (3x3)
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix2 = {
Faculty of Computing and Information Technology (FCIT) Department of
Computing Indus University, Karachi
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
System.out.println("\nMatrix 2:");
printMatrix(matrix2);
System.out.println("\nResultant Matrix:");
printMatrix(result);
}
if (cols1 != rows2) {
System.out.println("Matrix multiplication is not possible.");
return null;
}
return result;
}
Faculty of Computing and Information Technology (FCIT) Department of
Computing Indus University, Karachi
// Function to print a matrix
private static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
Output