Write a program to print upper triangular array of given array.
#include<stdio.h>
int main() {
int rows, cols, r, c, matrix[10][10];
printf("Please enter the number of rows for the matrix: ");
scanf("%d", &rows);
printf("\n");
printf("Please enter the number of columns for the matrix: ");
scanf("%d", &cols);
printf("\n");
printf("Please enter the elements for the Matrix: \n");
for(r = 0; r < rows; r++){
for(c = 0;c < cols;c++){
scanf("%d", &matrix[r][c]);
}
}
printf("\n\n The Upper Triangular Matrix is: ");
for(r = 0; r < rows; r++){
printf("\n");
for(c = 0; c < cols; c++){
if(r > c){
printf("0");
printf("\t");
}
else{
printf("%d\t ", matrix[r][c]);
}
}
}
return 0;
}
Output
Please enter the elements for the Matrix:
12 13 14
15 16 17
18 19 20
The Upper Triangular Matrix is:
12 13 14
0 16 17
0 0 20