C Program to Print Upper Triangular Matrix

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Write a C Program to Print Upper Triangular Matrix

Explanation Video:

Custom Image

Explanation:

 

An upper triangular matrix has all elements below the diagonal as zero.

Source Code:
#include <stdio.h>

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

    printf("Upper Triangular Matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++)
            if (i > j)
                printf("0 ");
            else
                printf("%d ", arr[i][j]);
        printf("\n");
    }
    return 0;
}
Output:
Upper Triangular Matrix:
1 2 3
0 5 6
0 0 9
Share this Article & Support Us:
Status
printf('Loading...');