This blog is under construction

Wednesday, 24 July 2013

C program to check lower triangular matrix

Write a C program to check lower triangular matrix.


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

  int main() {
        int i, j, order, temp = 0;
        int matrix[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", &matrix[i][j]);
                }
        }

        /* checking for lower triangular matrix */
        for (i = 0; i < order; i++) {
                for (j = 0; j < order; j++) {
                        if (j > i && matrix[i][j] != 0) {
                                temp = 1;
                                goto end;
                        }
                }
        }

  end:
        /* printing the result */
        if (temp) {
                printf("Given Matrix is not a lower triangular matrix!!\n");
        } else {
                printf("Given Matrix is a lower triangular matrix!!\n");
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of order:3
  Enter the matrix entries:
  1  0  0
  1  2  0
  1  2  3
  Given Matrix is a lower triangular matrix!!


No comments:

Post a Comment