Q:

C program to print the upper triangular matrix

0

C program to print the upper triangular matrix

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Given a 3x3 matrix, we have to print the upper triangular matrix using C program.

Program:

The source code to print the upper triangular matrix is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to print the upper triangular matrix

#include <stdio.h>

int main()
{
    int Matrix[3][3] = {
        { 9, 8, 7 },
        { 5, 4, 6 },
        { 1, 2, 3 }
    };

    int i, j;

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

    printf("\nUpper triangular matrix is: \n");
    for (i = 0; i < 3; i++) {

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

Output:

Matrix:
9 8 7 
5 4 6 
1 2 3 

Upper triangular matrix is: 
9 8 7 
  4 6 
    3 

Explanation:

Here, we created a 3X3 matrix that contains integer elements. Then we printed matrix elements and the upper triangular matrix on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< C program to find the trace of matrix...