In this tutorial you will learn about the C Program to find Upper Triangle Matrix and its application with practical example.
C Program to find Upper Triangle Matrix
In this tutorial, we will learn to create a C program that will find Upper Triangle Matrix using C programming.
Prerequisites
Before starting with the tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
What is a matrix?
A matrix is a multidimensional phenomenon and is created with the help of a 2d array having multiple rows and columns.
Algorithm:-
With the help of this program, we can find Upper Triangle Matrix.
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input size of matrix. 3. Taking the input elements of matrix. 4. Creating the <strong>Upper Triangle Matrix.</strong> 5. Printing the<strong> Upper Triangle Matrix</strong>. 6. End Program. |
Program to find Upper Triangle Matrix:-
In this tutorial, we will create a multidimensional array with the help of C programming. First, we will take the size of the array from the user, and then we will take the input elements. Now we will create an upper triangle in the matrix by adding a zero in rows. At last, we will print the matrix triangle.
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* C Program to find Upper Triangle Matrix */ #include<stdio.h> int main() { //declaring the required variables for the program. int i, j, rw, clm, a[10][10]; //i = it will hold the input value for the rows in program. //j = it will hold the input value for the columns in program. //Taking the input size of the matrix printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j); //Taking the matrix in input from the user. printf("\n Please Enter the Matrix Elements \n"); for(rw = 0; rw < i; rw++) { for(clm = 0;clm < j;clm++) { scanf("%d", &a[rw][clm]); } } //Creating the Upper Triangle Matrix by adding a zero value for(rw = 0; rw < i; rw++) { printf("\n"); for(clm = 0; clm < j; clm++) { if(clm >= rw) { printf("%d ", a[rw][clm]); } else { //printing the Upper Triangle Matrix printf("0 "); } } } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- a[10][10]= it will hold the integer value.
- rw = it will hold the integer value.
- clm = it will hold the integer value.
- i= it will hold the input integer value for rows.
- j= it will hold the input integer value for columns.
Taking the size and the elements of the matrix from the user.
Creating the upper right triangle from the matrix.
Printing the Upper Triangle Matrix.