Program to Initialize 3 x 3 Matrix and Print it
- Write a program to Initialize 3 x 3 Matrix and Print it in C
- Write a program to Initialize 3 x 3 Matrix and Print it in C++
- Write a program to Initialize 3 x 3 Matrix and Print it in Python
- Write a program to Initialize 3 x 3 Matrix and Print it in PHP
- Write a program to Initialize 3 x 3 Matrix and Print it in Java
- Write a program to Initialize 3 x 3 Matrix and Print it in Java Script
- Write a program to Initialize 3 x 3 Matrix and Print it in C#
Explanation:
Here is the logic to initialize and print a 3×3 matrix:
Steps:
- Define the Matrix: Declare a 2D array or list to represent a 3×3 matrix.
- Initialize the Matrix: Assign values to each element in the matrix during declaration or input them from the user.
- Print the Matrix:
- Use nested loops:
- Outer loop for rows.
- Inner loop for columns.
- Print each row of the matrix on a new line for proper formatting.
- Use nested loops:
Program to Initialize 3 x 3 Matrix and Print it
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("3x3 Matrix:\n");
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] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
cout << "3x3 Matrix:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("3x3 Matrix:")
for row in matrix:
print(" ".join(map(str, row)))
<?php
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
echo "3x3 Matrix:\n";
foreach ($matrix as $row) {
echo implode(" ", $row) . "\n";
}
?>
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("3x3 Matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log("3x3 Matrix:");
matrix.forEach(row => {
console.log(row.join(" "));
});
using System;
class Program {
static void Main() {
int[,] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Console.WriteLine("3x3 Matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}