C program to print reversed left hollow triangle pattern

In this program, we are going to print reversed left hollow triangle pattern. This program is similar to left hollow triangle pattern program in c.
To understand how to print this shape pattern, take a look at shape as shown below:

As said in name, only difference between left hollow triangle and reversed left hollow triangle is that it has bottom of triangle on first row and top at end.

Pre-requisites:

To be able to write this program, concepts like data types, variables, for loop, main method, input output functions and if else statement are necessary to be aware of.

Approach:

For this program, we are going to use two for loops.

  • Parent for loop or main for loop
  • Nested for loop or inner loop

Parent for loop will be used for rotating through rows and nested for loop will be used for rotating through columns.
To keep left hollow triangle shape pattern get displayed according to user, we will take input from user and set it as row limit so that if user input value 5 then pattern shape will be displayed over 5 rows on console.

Variable I, j are used as loop control variable which are either decremented or incremented to control loop execution count. Reversed left triangle pattern has 1st row which contains all star character So we have used condition here as I == 1 and also last column of every row has star character so that we have used j == 1 condition in if statement so for every last column of every row condition becomes true( for printing on last column, j == 1 condition because loop indexing starts from 5 so last column will be at index 1).

For middle rows only two star character are printed i.e. for first column ( j==n means star character on 1st column for first row, 2nd column for 2nd row and so on ) and last column(this comes under condition j == 1) of that row so that j == n condition is specified. Here n is counter value used to satisfy condition of printing star character on same row/same column.

When these condition will be false blank value will be printed there and at end of nested loop, counter n will be decremented and printf statement to add new line will be executed.

To know more about execution flow please refer execution table, algorithm and program explanation.

Program:

Output:

Program explanation:

Line 1 and 2 declares header files which includes predefined library functions which are used in program.
Line 3 is main function which is of integer type and program execution starting point.
Line 5 has loop control variables declared like I, j and variables rows to accept user input which will be set as limit in evaluate condition.
Line 7 and 8 are printf and scanf statement, which are used to print message asking user to provide input and scanf function reads the user input into variable specified.
Line 8 sets the value of rows to variable n. here n is used as counter which will be used then in if else statement condition.

Here 2 for loops are used.

Parent for loop which is used to iterate through rows. This loop iterates 1 to rows times. Loop structure as shown below:

nested for loop is used to iterate through columns. This loop iterates 0 to rows-1 times. In loop structure, loop control variable is initialized to rows and then decremented using post decrement operator after each iteration. Loop structure is as shown below:

Inside nested for loop is if else statement. If statement body contains printf statement to print star special character, but to get that statement executed loop must satisfy if condition where i is equal to 1 or j equal to 1 or j equal to n.

here I == 1 is used for printing star pattern on first row.
J == 1 is used for priting star pattern on last column.(j == 1 because j is initialized to rows so after decrement 1 is last column index of row)
J == n is used for printing star pattern on connecting row (backward slash shape). Here we have decremented counter n after every parent loop iteration so that we can match column index with row index.

After closing nested for loop is printf statement to add new line.
Line 25 is return 0 statement which returns 0 indicating successful execution.
For more details about real time execution please refer the execution table.

Leave a Reply

%d bloggers like this: