C Program to Print Upper and Lower Triangle of Matrix

C Program to Print Upper and Lower Triangle of Matrix

Write C Program to Print Upper and Lower Triangle of Matrix

// C Program to Print Upper and Lower Triangle of Matrix

#include <stdio.h>

void print_uppertriangle(int A[3][3],int i,int j)
{
    for(i = 0; i < 3; i++)          //to print the upper triangle
    {
        for(j = 0; j < 3;j ++)
        {
            if(i <= j)
            {
                printf("%d", A[i][j]);
	    }
	    else
	    {
                printf("%d", 0);
            }
	    printf(" ");
	}
        printf("\n");
    }
}

void print_lowertriangle(int A[3][3],int i,int j)
{
    for(i = 0; i < 3; i++)          //to print the upper triangle
    {
        for(j = 0; j < 3; j++)
        {
            if(i >= j)
	    {
                printf("%d", A[i][j]);
            }
	    else
	    {
                printf("%d", 0);
            }
	    printf(" ");
        }
	printf("\n");
    }
}

int main()
{
    int i,j;
    int A[3][3] = {{2, 1, 6}, {9, 5, 1}, {4, 3, 8} };

    printf("Matrix A :--> \n");
    for(i = 0;i < 3; i++)
    {
        for( j = 0; j < 3; j++)
	{
	    printf("%d  ", A[i][j]);
	}
	printf("\n");
    }

    printf("Upper Triangular Matrix :--> \n");
    print_uppertriangle(A, i, j);

    printf("\nLower Triangular Matrix :--> \n");
    print_lowertriangle(A, i, j);

    return 0;
}

Output:

Matrix A :-->
2  1  6
9  5  1
4  3  8

Upper Triangular Matrix :-->
2 1 6
0 5 1
0 0 8

Lower Triangular Matrix :-->
2 0 0
9 5 0
4 3 8