Write a program to Scan and Print 3 x 3 Matrix in C
Write a program to Scan and Print 3 x 3 Matrix in C++
Write a program to Scan and Print 3 x 3 Matrix in Python
Write a program to Scan and Print 3 x 3 Matrix in PHP
Write a program to Scan and Print 3 x 3 Matrix in Java
Write a program to Scan and Print 3 x 3 Matrix in Java Script
Write a program to Scan and Print 3 x 3 Matrix in C#
Explanation:
Steps:
Define the Matrix: Declare a 2D array or list to hold 3×3 elements.
Scan the Matrix:
Use nested loops to take input for each element of the matrix.
The outer loop iterates through rows.
The inner loop iterates through columns.
Print the Matrix:
Use nested loops to display the matrix in the same format as entered.
Program to Scan and Print 3 x 3 Matrix
C
C++
Python
PHP
JAVA
Java Script
C#
#include <stdio.h>
int main() {
int matrix[3][3];
printf("Enter elements for a 3x3 matrix:\n");
// Input the matrix elements
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", &matrix[i][j]);
}
}
printf("\n3x3 Matrix:\n");
// Print the matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int matrix[3][3];
cout << "Enter elements for a 3x3 matrix:" << endl;
// Input the matrix elements
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter element [" << i + 1 << "][" << j + 1 << "]: ";
cin >> matrix[i][j];
}
}
cout << "\n3x3 Matrix:" << endl;
// Print the matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
matrix = []
print("Enter elements for a 3x3 matrix:")
# Input the matrix elements
for i in range(3):
row = []
for j in range(3):
value = int(input(f"Enter element [{i+1}][{j+1}]: "))
row.append(value)
matrix.append(row)
print("\n3x3 Matrix:")
# Print the matrix
for row in matrix:
print(" ".join(map(str, row)))
<?php
$matrix = array();
echo "Enter elements for a 3x3 matrix:\n";
// Input the matrix elements
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$matrix[$i][$j] = (int)readline("Enter element [" . ($i+1) . "][" . ($j+1) . "]: ");
}
}
echo "\n3x3 Matrix:\n";
// Print the matrix
foreach ($matrix as $row) {
echo implode(" ", $row) . "\n";
}
?>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[][] matrix = new int[3][3];
Scanner scanner = new Scanner(System.in);
System.out.println("Enter elements for a 3x3 matrix:");
// Input the matrix elements
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Enter element [" + (i + 1) + "][" + (j + 1) + "]: ");
matrix[i][j] = scanner.nextInt();
}
}
System.out.println("\n3x3 Matrix:");
// Print the matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}
let matrix = [];
console.log("Enter elements for a 3x3 matrix:");
// Input the matrix elements
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);
}
matrix.push(row);
}
console.log("\n3x3 Matrix:");
// Print the matrix
matrix.forEach(row => {
console.log(row.join(" "));
});
using System;
class Program {
static void Main() {
int[,] matrix = new int[3, 3];
Console.WriteLine("Enter elements for a 3x3 matrix:");
// Input the matrix elements
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write($"Enter element [{i+1}][{j+1}]: ");
matrix[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n3x3 Matrix:");
// Print the matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}