0% found this document useful (0 votes)
10 views2 pages

C Program to Display 2D Matrix

Uploaded by

cneelam724
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

C Program to Display 2D Matrix

Uploaded by

cneelam724
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Write a C program to display a 2-dimensional matrix.

#include <stdio.h>

int main() {
int rows, cols;

// Input number of rows and columns


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

int matrix[rows][cols];

// Input elements of the matrix


printf("Enter elements of the matrix:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}

// Display the matrix


printf("\nThe 2D matrix is:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

return 0;
}

You might also like