C Program to check whether Matrix is upper triangular matrix or not

By | 18.11.2016

Check upper triangular matrix


Write a C Program to check whether a matrix is upper triangular matrix or not. Here’s simple program to check if matrix is upper triangular matrix. C program to check upper triangular matrix.


What is Matrix ?


Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

  • type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.


Below is the source code for C Program to check whether a matrix is upper triangular matrix or not which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to check whether a matrix is upper triangular matrix or not */

#include <stdio.h>

int main()
{
    int A[10][10],i,j,m,n;
    int row, col, isUpper;

    printf("Enter no. of rows :: ");
        scanf("%d", &m);
        printf("\nEnter no. of cols :: ");
        scanf("%d",&n);
        printf("\nEnter values to the matrix :: \n");
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                 printf("\nEnter a[%d][%d] value :: ",i,j);
                 scanf("%d", &A[i][j]);
        }
    }

    printf("\nThe given matrix is :: \n\n");

        for (i = 0; i < m; ++i)
        {
            for (j = 0; j < n; ++j)
            {
                printf("\t%d", A[i][j]);
            }
            printf("\n\n");
        }

    /*
     * Checks whether the matrix is Upper triangular
     */
    isUpper = 1;
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            /*
             * If elements below the main diagonal (col<row)
             * is not equal to zero then it is not upper triangular matrix
             */
            if(col<row && A[row][col]!=0)
            {
                isUpper = 0;
            }
        }
    }

    /*
     * If it is upper triangular matrix
     */
    if(isUpper==1)
    {
        printf("\nThis is a Upper triangular matrix.\n");

        for(row=0; row<m; row++)
        {
            for(col=0; col<n; col++)
            {
                if(A[row][col] != 0)
                {
                    printf("\t%d ", A[row][col]);
                }
                else
                {
                    printf("\t");
                }

            }

            printf("\n\n");
        }
    }
    else
    {
        printf("\nThis is Not a Upper triangular matrix.");
    }

    return 0;
}

Output : :


/*  C Program to check whether a matrix is upper triangular matrix or not */

Enter no. of rows :: 3

Enter no. of cols :: 3

Enter values to the matrix ::

Enter a[0][0] value :: 1

Enter a[0][1] value :: 2

Enter a[0][2] value :: 3

Enter a[1][0] value :: 0

Enter a[1][1] value :: 4

Enter a[1][2] value :: 5

Enter a[2][0] value :: 0

Enter a[2][1] value :: 0

Enter a[2][2] value :: 6

The given matrix is ::

        1       2       3

        0       4       5

        0       0       6


This is a Upper triangular matrix.
        1       2       3

                4       5

                        6


Process returned 0

Above is the source code for C Program to check whether a matrix is upper triangular matrix or not which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4.2 5 votes
Article Rating
Category: C Programming Matrix Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments