Upper triangular matrix or not

Write a program to check the upper triangular matrix or not.

Upper Triangular: A square matrix whose all elements below the main diagonal are zero.

Example:

Input:  matrix[][4]={{1,2,3,6},{0,1,2,3},{0,0,4,2},{0,0,0,3}}
Output: Upper Triangular Matrix

C Program

#include <stdio.h>

int main()
{
    int matrix[][4] = {{1236}, {0123}, {0042}, {0003}};

    int n = 4;
    int ans = 0;
    for (int i = 0i < ni++)
    {
        int flag = 0;
        for (int j = 0j < ij++)
        {
            if (matrix[i][j] != 0)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            ans = 1;
            break;
        }
    }
    if (ans == 0)
    {
        printf("Upper Triangular Matrix\n");
    }
    else
    {
        printf("Not a Upper Triangular Matrix\n");
    }
    return 0;
}


No comments:

Post a Comment