Friday 10 November 2017

C program to find upper triangular matrix


Code

#include
#define MAX_ROWS 3
#define MAX_COLS 3

int main()
{
    int array[MAX_ROWS][MAX_COLS];
    int row, col, isUpper;

    /* Input elements in matrix from user */
    printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS);
    for(row=0; row
    {
        for(col=0; col
        {
            scanf("%d", &array[row][col]);
        }
    }
    
    /* Check Upper triangular matrix condition */
    isUpper = 1;
    for(row=0; row
    {
        for(col=0; col
        {
            /*
             * If elements below the main diagonal (col
             * is not equal to zero then it is not upper triangular matrix
             */
            if(col
            {
                isUpper = 0;
            }
        }
    }
    
    /* Print elements of upper triangular matrix  */
    if(isUpper == 1)
    {
        printf("\nThe matrix is Upper triangular matrix.\n");

        for(row=0; row
        {
            for(col=0; col
            {
                printf("%d ", array[row][col]);
            }

            printf("\n");
        }
    }
    else
    {
        printf("\nThe matrix is not Upper triangular matrix.");
    }

    return 0;
}

Output

Enter elements in matrix of size 3x3:
1 2 3
0 5 6
0 0 9

The matrix is Upper triangular matrix.
1 2 3
0 5 6
0 0 9

More C programs










100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...