Saturday, 10 January 2015

C Program to display Upper Triangular Matrix



This is a C program which display the upper triangular form of a given matrix.

To display the upper triangular form of a given matrix we make all the elements below the diagonal row as zero.




See the figure below,
PROGRAM :
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows,columns,i,j;
    printf("Enter order of matrix  \t:");
    scanf("%d%d",&rows,&columns);
    int matrix[rows][columns];
    printf("\n\nEnter elements of a matrix\n");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("Enter element m%d%d: ",i+1,j+1);
            scanf("%d",&matrix[i][j]);
        }
    }
    printf("\n\nMATRIX is :\n");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("%d\t",matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n\nIts Upper Triangular form is: \n");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            if(i<=j)
            printf("%d\t",matrix[i][j]);
            else
                printf("%d\t",0);
        }
        printf("\n");
    }
    return 0;
}

OUTPUT :
C -Upper Triangular MAtrix