Program to Add Two Matrices of Size 3 X 3
- Write a program to Add Two Matrices of Size 3 X 3 in C
- Write a program to Add Two Matrices of Size 3 X 3 in C++
- Write a program to Add Two Matrices of Size 3 X 3 in Python
- Write a program to Add Two Matrices of Size 3 X 3 in PHP
- Write a program to Add Two Matrices of Size 3 X 3 in Java
- Write a program to Add Two Matrices of Size 3 X 3 in Java Script
- Write a program to Add Two Matrices of Size 3 X 3 in C#
Explanation:
The total of the corresponding elements in the two matrices must be calculated and stored in a third matrix in order to add two matrices of size 3 × 3 3 × 3.
Steps:
- Define Matrices:
- Declare two 3×3 matrices (A and B) for input.
- Declare a third 3×3 matrix (C) to store the result.
- Add the Matrices:
- Use nested loops to iterate through each element of the matrices.
- For each position (i, j), compute: C[i][j]=A[i][j]+B[i][j]
- Display the Result:
- Use nested loops to print the resultant matrix.
Program to Add Two Matrices of Size 3 X 3.
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int matrix1[3][3], matrix2[3][3], sum[3][3];
// Input elements of first matrix
printf("Enter elements for the first 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
// Input elements of second matrix
printf("\nEnter elements for the second 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
// Adding the matrices
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print the result
printf("\nSum of the two matrices:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int matrix1[3][3], matrix2[3][3], sum[3][3];
// Input elements of first matrix
cout << "Enter elements for the first 3x3 matrix:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter element [" << i + 1 << "][" << j + 1 << "]: ";
cin >> matrix1[i][j];
}
}
// Input elements of second matrix
cout << "\nEnter elements for the second 3x3 matrix:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter element [" << i + 1 << "][" << j + 1 << "]: ";
cin >> matrix2[i][j];
}
}
// Adding the matrices
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print the result
cout << "\nSum of the two matrices:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << sum[i][j] << " ";
}
cout << endl;
}
return 0;
}
matrix1 = []
matrix2 = []
print("Enter elements for the first 3x3 matrix:")
for i in range(3):
row = []
for j in range(3):
value = int(input(f"Enter element [{i+1}][{j+1}]: "))
row.append(value)
matrix1.append(row)
print("\nEnter elements for the second 3x3 matrix:")
for i in range(3):
row = []
for j in range(3):
value = int(input(f"Enter element [{i+1}][{j+1}]: "))
row.append(value)
matrix2.append(row)
# Adding the matrices
sum_matrix = []
for i in range(3):
row = []
for j in range(3):
row.append(matrix1[i][j] + matrix2[i][j])
sum_matrix.append(row)
# Print the result
print("\nSum of the two matrices:")
for row in sum_matrix:
print(" ".join(map(str, row)))
<?php
$matrix1 = array();
$matrix2 = array();
$sum = array();
// Input elements for first matrix
echo "Enter elements for the first 3x3 matrix:\n";
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$matrix1[$i][$j] = (int)readline("Enter element [" . ($i+1) . "][" . ($j+1) . "]: ");
}
}
// Input elements for second matrix
echo "\nEnter elements for the second 3x3 matrix:\n";
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$matrix2[$i][$j] = (int)readline("Enter element [" . ($i+1) . "][" . ($j+1) . "]: ");
}
}
// Adding the matrices
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$sum[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];
}
}
// Print the result
echo "\nSum of the two matrices:\n";
for ($i = 0; $i < 3; $i++) {
echo implode(" ", $sum[$i]) . "\n";
}
?>
import java.util.Scanner;
public class MatrixAddition {
public static void main(String[] args) {
int[][] matrix1 = new int[3][3];
int[][] matrix2 = new int[3][3];
int[][] sum = new int[3][3];
Scanner scanner = new Scanner(System.in);
// Input elements for first matrix
System.out.println("Enter elements for the first 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Enter element [" + (i + 1) + "][" + (j + 1) + "]: ");
matrix1[i][j] = scanner.nextInt();
}
}
// Input elements for second matrix
System.out.println("\nEnter elements for the second 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Enter element [" + (i + 1) + "][" + (j + 1) + "]: ");
matrix2[i][j] = scanner.nextInt();
}
}
// Adding the matrices
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print the result
System.out.println("\nSum of the two matrices:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}
let matrix1 = [];
let matrix2 = [];
let sum = [];
console.log("Enter elements for the first 3x3 matrix:");
for (let i = 0; i < 3; i++) {
let row = [];
for (let j = 0; j < 3; j++) {
let value = parseInt(prompt(`Enter element [${i+1}][${j+1}]:`));
row.push(value);
}
matrix1.push(row);
}
console.log("\nEnter elements for the second 3x3 matrix:");
for (let i = 0; i < 3; i++) {
let row = [];
for (let j = 0; j < 3; j++) {
let value = parseInt(prompt(`Enter element [${i+1}][${j+1}]:`));
row.push(value);
}
matrix2.push(row);
}
// Adding the matrices
for (let i = 0; i < 3; i++) {
let row = [];
for (let j = 0; j < 3; j++) {
row.push(matrix1[i][j] + matrix2[i][j]);
}
sum.push(row);
}
// Print the result
console.log("\nSum of the two matrices:");
sum.forEach(row => {
console.log(row.join(" "));
});
using System;
class MatrixAddition {
static void Main() {
int[,] matrix1 = new int[3, 3];
int[,] matrix2 = new int[3, 3];
int[,] sum = new int[3, 3];
// Input elements for first matrix
Console.WriteLine("Enter elements for the first 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write($"Enter element [{i+1}][{j+1}]: ");
matrix1[i, j] = int.Parse(Console.ReadLine());
}
}
// Input elements for second matrix
Console.WriteLine("\nEnter elements for the second 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write($"Enter element [{i+1}][{j+1}]: ");
matrix2[i, j] = int.Parse(Console.ReadLine());
}
}
// Adding the matrices
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i, j] = matrix1[i, j] + matrix2[i, j];
}
}
// Print the result
Console.WriteLine("\nSum of the two matrices:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write(sum[i, j] + " ");
}
Console.WriteLine();
}
}
}