Program to Multiply Two Matrices of Size 3 X 3
- Write a program to Multiply Two Matrices of Size 3 X 3 in C
- Write a program to Multiply Two Matrices of Size 3 X 3 in C++
- Write a program to Multiply Two Matrices of Size 3 X 3 in Python
- Write a program to Multiply Two Matrices of Size 3 X 3 in PHP
- Write a program to Multiply Two Matrices of Size 3 X 3 in Java
- Write a program to Multiply Two Matrices of Size 3 X 3 in Java Script
- Write a program to Multiply Two Matrices of Size 3 X 3 in C#
Explanation:
You must calculate the dot product of the rows of the first matrix with the columns of the second matrix in order to multiply two matrices of size 3 × 3 . The outcome is kept in a third 3 × 3 matrix.
Steps:
- Define Matrices:
- Declare two 3×3 matrices (A and B) as input.
- Declare a third 3×3 matrix (C) to store the result.
- Matrix Multiplication:
- For each element in the resultant matrix C[i][j], compute: C[i][j]=∑A[i][k]×B[k][j]
- Use nested loops:
- Outer loops iterate over rows of A and columns of B.
- Inner loop calculates the dot product.
- Display the Result:
- Use nested loops to print the resultant matrix.
Program to Multiply Two Matrices of Size 3 X 3
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int A[3][3], B[3][3], C[3][3];
// Input elements for the first matrix A
printf("Enter elements for matrix A (3x3):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Enter element A[%d][%d]: ", i + 1, j + 1);
scanf("%d", &A[i][j]);
}
}
// Input elements for the second matrix B
printf("Enter elements for matrix B (3x3):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Enter element B[%d][%d]: ", i + 1, j + 1);
scanf("%d", &B[i][j]);
}
}
// Multiply matrices A and B
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
C[i][j] = 0;
for (int k = 0; k < 3; k++) {
C[i][j] += A[i][k] * B[k][j]; // Matrix multiplication
}
}
}
// Print the result
printf("\nResulting Matrix C (A * B):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int A[3][3], B[3][3], C[3][3];
// Input elements for the first matrix A
cout << "Enter elements for matrix A (3x3):" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter element A[" << i + 1 << "][" << j + 1 << "]: ";
cin >> A[i][j];
}
}
// Input elements for the second matrix B
cout << "Enter elements for matrix B (3x3):" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter element B[" << i + 1 << "][" << j + 1 << "]: ";
cin >> B[i][j];
}
}
// Multiply matrices A and B
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
C[i][j] = 0;
for (int k = 0; k < 3; k++) {
C[i][j] += A[i][k] * B[k][j]; // Matrix multiplication
}
}
}
// Print the result
cout << "\nResulting Matrix C (A * B):" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}
A = []
print("Enter elements for matrix A (3x3):")
for i in range(3):
row = []
for j in range(3):
value = int(input(f"Enter element A[{i+1}][{j+1}]: "))
row.append(value)
A.append(row)
# Input elements for the second matrix B
B = []
print("Enter elements for matrix B (3x3):")
for i in range(3):
row = []
for j in range(3):
value = int(input(f"Enter element B[{i+1}][{j+1}]: "))
row.append(value)
B.append(row)
# Multiply matrices A and B
C = [[0 for _ in range(3)] for _ in range(3)]
for i in range(3):
for j in range(3):
for k in range(3):
C[i][j] += A[i][k] * B[k][j] # Matrix multiplication
# Print the result
print("\nResulting Matrix C (A * B):")
for row in C:
print(row)
<?php
// Input elements for the first matrix A
$A = array();
echo "Enter elements for matrix A (3x3):\n";
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$A[$i][$j] = (int)readline("Enter element A[" . ($i+1) . "][" . ($j+1) . "]: ");
}
}
// Input elements for the second matrix B
$B = array();
echo "Enter elements for matrix B (3x3):\n";
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$B[$i][$j] = (int)readline("Enter element B[" . ($i+1) . "][" . ($j+1) . "]: ");
}
}
// Multiply matrices A and B
$C = array();
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$C[$i][$j] = 0;
for ($k = 0; $k < 3; $k++) {
$C[$i][$j] += $A[$i][$k] * $B[$k][$j]; // Matrix multiplication
}
}
}
// Print the result
echo "\nResulting Matrix C (A * B):\n";
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
echo $C[$i][$j] . " ";
}
echo "\n";
}
?>
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] A = new int[3][3];
int[][] B = new int[3][3];
int[][] C = new int[3][3];
Scanner scanner = new Scanner(System.in);
// Input elements for the first matrix A
System.out.println("Enter elements for matrix A (3x3):");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Enter element A[" + (i + 1) + "][" + (j + 1) + "]: ");
A[i][j] = scanner.nextInt();
}
}
// Input elements for the second matrix B
System.out.println("Enter elements for matrix B (3x3):");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Enter element B[" + (i + 1) + "][" + (j + 1) + "]: ");
B[i][j] = scanner.nextInt();
}
}
// Multiply matrices A and B
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
C[i][j] = 0;
for (int k = 0; k < 3; k++) {
C[i][j] += A[i][k] * B[k][j]; // Matrix multiplication
}
}
}
// Print the result
System.out.println("\nResulting Matrix C (A * B):");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(C[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}
let A = [];
let B = [];
let C = [];
console.log("Enter elements for matrix A (3x3):");
for (let i = 0; i < 3; i++) {
let row = [];
for (let j = 0; j < 3; j++) {
let value = parseInt(prompt(`Enter element A[${i+1}][${j+1}]:`));
row.push(value);
}
A.push(row);
}
console.log("Enter elements for matrix B (3x3):");
for (let i = 0; i < 3; i++) {
let row = [];
for (let j = 0; j < 3; j++) {
let value = parseInt(prompt(`Enter element B[${i+1}][${j+1}
using System;
class MatrixMultiplication
{
static void Main()
{
int[,] A = new int[3, 3];
int[,] B = new int[3, 3];
int[,] C = new int[3, 3];
// Input elements for the first matrix A
Console.WriteLine("Enter elements for matrix A (3x3):");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"Enter element A[{i + 1}][{j + 1}]: ");
A[i, j] = int.Parse(Console.ReadLine());
}
}
// Input elements for the second matrix B
Console.WriteLine("Enter elements for matrix B (3x3):");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"Enter element B[{i + 1}][{j + 1}]: ");
B[i, j] = int.Parse(Console.ReadLine());
}
}
// Multiply matrices A and B
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
C[i, j] = 0;
for (int k = 0; k < 3; k++)
{
C[i, j] += A[i, k] * B[k, j]; // Matrix multiplication
}
}
}
// Print the result
Console.WriteLine("\nResulting Matrix C (A * B):");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(C[i, j] + " ");
}
Console.WriteLine();
}
}
}