This blog is under construction

Wednesday 24 July 2013

C program to print lower triangular matrix

Write a C program to print lower triangular matrix.


  #include <stdio.h>
  #define MAXROWS 10
  #define MAXCOLS 10

  int main() {
        int i, j, order;
        int mat1[MAXROWS][MAXCOLS];

        /* get the order of the matrix from the user */
        printf("Enter the number of order:");
        scanf("%d", &order);

        /* Boundary Check */
        if (order > MAXROWS || order < 0) {
                printf("Boundary Level Exceeded!!\n");
                return 0;
        }

        /* get the entries for the input matrix */
        printf("\nEnter the matrix entries:\n");
        for (i = 0; i < order; i++) {
                for (j = 0; j < order; j++) {
                        scanf("%d", &mat1[i][j]);
                }
        }

        /* printing for lower triangular matrix */
        printf("\n\nLower triangular matrix for the given input:\n");
        for (i = 0; i < order; i++) {
                for (j = 0; j < order; j++) {
                        if (j > i) {
                                printf("0  ");
                        } else {
                                printf("%d  ", mat1[i][j]);
                        }
                }
                printf("\n");
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of order:3
  Enter the matrix entries:
  1  2  3
  4  5  6
  7  8  9

  Lower triangular matrix for the given input:
  1  0  0  
  4  5  0  
  7  8  9  


No comments:

Post a Comment