C program to print the upper triangular matrix

Here, we are going to learn how to print the upper triangular matrix in C programming language?
Submitted by Nidhi, on July 15, 2021

Problem Solution:

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.

C Two-dimensional Arrays Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.