Blogger templates

Pages

Tuesday, 3 July 2012

WAP of C language to Print the Matrix of Fixed or Desired Order

Matrix
Statement of C Program: This Program Reads and prints the elements of a Matrix:

#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[9][9] ;                           /* Maximum Elements in the Matrix is 81 */
int matrix[3][3];                                      /* matrix of 9 elements */
int i, j;                                        /* 'i' used for rows and 'j' used for columns */
clrscr();

printf(" Enter the elements of Matrix\n");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf("%d" , &matrix[i][j] );
}
}                          /* Elements of Matrix Read (Input) with the help of scanf() Function */


printf(" Matrix is:\n");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
printf("%d\t" , matrix[i][j] );                           /* '\t' used for Tab */
}                                                                    
printf("\n");                                          /* '\n' used for next line character */
}

getch();
}                                                                    /* End of main */


 Output:
 Enter the elements of Matrix
 9
 8
 7
 6
 5
 4
 3
 2
 1
 Matrix is:
 9   8   7
 6   5   4
 3   2   1


=> Note:  If you want to Print the Matrix of any Order then make few changes (Blue Colour) in the above Matrix. 

#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[9][9];                                  /* Matrix of 81 Elements */
int i, j;
int Row , Column ;
clrscr();

printf(" Enter the order of Matrix\n");
scanf("%d%d" , &Row , &Column);

printf("Enter the elements of Matrix\n");
for(i=0 ; i<Row ; i++)
{
for(j=0 ; j<Column ; j++)
{
scanf("%d" , &matrix[i][j] );
}
}

printf(" Matrix is:\n");
for(i=0 ; i<Row ; i++)
{
for(j=0 ; j<Column ; j++)
{
printf("%d\t" , matrix[i][j] );
}
printf("\n");
}
getch();
}

 Output:
 Enter the order of Matrix
 3  3
 Enter the elements of Matrix
 9
 8
 7
 6
 5
 4
 3
 2
 1
 Matrix is:
 9   8   7
 6   5   4
 3   2   1

                                                                     That's All

0 comments:

Post a Comment