Saturday 28 June 2014

C++ program to check whether matrix is upper/lower triangular or not

Here is a C++ program to check whether matrix is upper/lower triangular or not. First program simply checks whether matrix is upper triangular or not and second program checks whether matrix is lower triangular or not.


C++ program to check whether matrix is upper triangular or not
#include<iostream>
using namespace std;
int main()
{
    int a[10][10],order;
    cout<<"Enter order of matrix (max 10*10): ";
    cin>>order;
    cout<<"Enter matrix a1 : "<<endl;
    for(int i=0;i<order;i++)
        for(int j=0;j<order;j++)
           cin>>a[i][j];
    for(int i=1;i<order-1;i++)
       for(int j=0;j<i;j++)
            if(a[i][j]!=0)
            {
                cout<<"Matrix is not upper triangular matrix";
                return 0;
            }
    cout<<"Matrix is upper triangular matrix "<<endl;
    return 0;
}

OUTPUT




C++ program to check whether matrix is lower triangular or not
#include<iostream>
using namespace std;
int main()
{
    int a[10][10],order;
    cout<<"Enter order of matrix (max 10*10): ";
    cin>>order;
    cout<<"Enter matrix a1 : "<<endl;
    for(int i=0;i<order;i++)
        for(int j=0;j<order;j++)
           cin>>a[i][j];
    for(int i=0;i<order-1;i++)
       for(int j=i+1;j<order;j++)
            if(a[i][j]!=0)
            {
                cout<<"Matrix is not lower triangular matrix";
                return 0;
            }
    cout<<"Matrix is lower triangular matrix "<<endl;
    return 0;
}

OUTPUT




No comments:

Post a Comment