Introduction

In linear algebra, triangular matrices are special types of square matrices. An upper triangular matrix is a matrix in which all the entries below the main diagonal are zero. Conversely, a lower triangular matrix is one where all the entries above the main diagonal are zero. These matrices are significant in solving linear equations, performing matrix factorizations, and simplifying many mathematical computations.

#include <stdio.h>

#define MAX 10  // Maximum size of the matrix

void inputMatrix(int matrix[MAX][MAX], int size) {
    printf("Enter elements of the matrix (%d x %d):\n", size, size);
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

int isUpperTriangular(int matrix[MAX][MAX], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < i; j++) {
            if (matrix[i][j] != 0) {
                return 0;  // Not upper triangular
            }
        }
    }
    return 1;  // Upper triangular
}

int isLowerTriangular(int matrix[MAX][MAX], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (matrix[i][j] != 0) {
                return 0;  // Not lower triangular
            }
        }
    }
    return 1;  // Lower triangular
}

void printMatrix(int matrix[MAX][MAX], int size) {
    printf("Matrix (%d x %d):\n", size, size);
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[MAX][MAX];
    int size;

    printf("Enter the size of the matrix: ");
    scanf("%d", &size);

    inputMatrix(matrix, size);
    
    printf("Original Matrix:\n");
    printMatrix(matrix, size);
    
    if (isUpperTriangular(matrix, size)) {
        printf("The matrix is upper triangular.\n");
    } else if (isLowerTriangular(matrix, size)) {
        printf("The matrix is lower triangular.\n");
    } else {
        printf("The matrix is neither upper nor lower triangular.\n");
    }

    return 0;
}

Explanation

  1. Includes and Defines: The program includes the stdio.h header for input/output functions and defines a constant MAX for the maximum size of the matrix.
  2. Input Function: The inputMatrix function prompts the user to enter the elements of a square matrix of size sizeƗsizesize \times sizesizeƗsize.
  3. Upper Triangular Check: The isUpperTriangular function checks if the matrix is upper triangular by verifying that all elements below the main diagonal (where i>ji > ji>j) are zero.
  4. Lower Triangular Check: The isLowerTriangular function checks if the matrix is lower triangular by ensuring that all elements above the main diagonal (where j>ij > ij>i) are zero.
  5. Print Function: The printMatrix function displays the matrix in a formatted way.
  6. Main Function: In main, the program:
    • Declares a matrix.
    • Asks for the size of the matrix.
    • Calls the input, print, and check functions to display the results.

Input

The user is prompted to enter the size of the matrix, followed by the matrix elements. Example input:

Enter the size of the matrix: 3
Enter elements of the matrix (3 x 3):
1 2 3
0 4 5
0 0 6

Output

The program will output whether the matrix is upper or lower triangular. For the example input, the output will be:

Original Matrix:
1 2 3 
0 4 5 
0 0 6 

The matrix is upper triangular.

Conclusion

This C program effectively demonstrates how to check if a matrix is upper or lower triangular. Understanding triangular matrices is crucial for various applications in mathematics and computer science, including solving systems of linear equations and performing matrix decompositions. The code can be easily adapted for larger matrices or different data types, making it a useful tool for exploring matrix properties in C.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights