fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int rows, cols;
  5.  
  6. // Ask user for matrix size
  7. printf("Enter number of rows: ");
  8. scanf("%d", &rows);
  9.  
  10. printf("Enter number of columns: ");
  11. scanf("%d", &cols);
  12.  
  13. int matrix[rows][cols]; // declare matrix
  14.  
  15. // Input elements
  16. printf("\nEnter elements of the matrix:\n");
  17. for(int i = 0; i < rows; i++) {
  18. for(int j = 0; j < cols; j++) {
  19. printf("Element [%d][%d]: ", i + 1, j + 1);
  20. scanf("%d", &matrix[i][j]);
  21. }
  22. }
  23.  
  24. // Display the matrix
  25. printf("\nMatrix you entered:\n");
  26. for(int i = 0; i < rows; i++) {
  27. for(int j = 0; j < cols; j++) {
  28. printf("%d\t", matrix[i][j]);
  29. }
  30. printf("\n");
  31. }
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 5320KB
stdin
 
stdout
Enter number of rows: Enter number of columns: 
Enter elements of the matrix:

Matrix you entered: