Friday, February 5, 2016

C program to read and display 3X3 integer Matrix using 2-D array.

#include<stdio.h>
int main()
{
  int row,col, arr[3][3];
  //Logic to read matrix
  for(row=0; row<3; row++) // rows
  {
    for(col=0; col<3; col++) //columns
    {
       printf("Enter matrix number[%d][%d]:", row, col);
       scanf("%d", &arr[row][col]);
    }
  }
  //Logic to print matrix
  printf("The 3 X 3 matrix:\n");
  for(row=0; row<3; row++) // rows
  {
    for(col=0; col<3; col++) //columns
    {
       printf("%d ", arr[row][col]);
    }
    printf("\n");
  }
  return 0;
}

Output of program

Enter matrix number[0][0]:1
Enter matrix number[0][1]:2
Enter matrix number[0][2]:3
Enter matrix number[1][0]:4
Enter matrix number[1][1]:5
Enter matrix number[1][2]:6
Enter matrix number[2][0]:7
Enter matrix number[2][1]:8
Enter matrix number[2][2]:9
The 3 X 3 matrix:
1 2 3
4 5 6
7 8 9



No comments:

Post a Comment