In this program we are going to print left hollow triangle pattern using star/asterisk in c language. The left hollow triangle shape looks as shown below:
Before starting to write the program, we should know few topics used to implement this program.
for loop, main method, input output functions.
If you have already checked out similar program left triangle star pattern, it will be easy to catch on to logic easily. The only different between these two patterns is blank spaces added in between and to just show all sides of triangle only to create a hollow shape.
Here we going to make use of three for loops. First will be the main for loop inside that we will add two nested for loops i.e. another for inside a for loop.
First main for loop or outer for loop will be used to iterate through rows. Second and third nested for loop will be used for printing blankspace and star pattern character respectively by iterating through columns.
User entered value will be accepted into a variables say rows and will be used to set limit in for loops to display that many rows in output.
To know more about logic and execution, follow up with algorithm or program explanation or execution table.
printf("Enter the no. of rows:\n");
if( j == 1 || j == i || i == rows)
On line 1 & 2, library files have been included which are stdio.h and conio.h. these are header files which has declaration and definition of functions used in main program.
Line 3 is where program execution starts. Main method in every program is main entry point for execution.
Next, we have declared few variables like loop control variables like I, j and variable rows to accept user provided input.
Line 6 & 7 are printf and scanf statements. These are input output functions which are defined inside header files. Printf statement is used to print a message on console specified inside double quotes inside printf(). Scanf() statement is used to accept values inside variable. To check more about these functions, check out input output functions in c.
Line 8 and 10, we have for loop used for iterating through rows and columns respectively to print desired pattern based on successful condition specified in for loop. First loop used is main loop or parent loop, this is initialized to 1 (i=1). This loop iterates through row rows times (condition here is I <= rows).
Loop structure looks like :
. I++ is postfix increment of loop control variable I, which is used for controlling how many times loop should iterate.
Inside main loop, we have nested for loop which is used to iterate through columns and print desired pattern. Nested for loops iterate through columns for every row when we put them inside main loop. Here for loop control variable j is initialized to rows i.e. j=rows. This loop iterates particular no. of times based on condition becoming true. Condition set here is j should be greater than 0 i.e. j > 0 means loop iterates through columns in decreasing no. of times as we have initialized j to rows and then we are decrementing j at time of leaving iteration ( j – – ).
To know more about how these for loop are used for printing pattern, please refer to execution table for real time values.
On line 12, Inside nested for loop body, if statement is used to specify multiple conditions and based on those condition being successful printf statement to display pattern character will be executed otherwise printf statement to display blank value in body of else will be executed.
if( j == 1 || j == i || i == rows ) || is or operator which separate conditions and if any one of condition is true then loop successfully enters loop body.
first condition, j == 1 means print (*)pattern values for 1st column of every row, j == i means print (*)pattern values for titled line that joins horizontal and vertical line i.e. here for middle rows we want to display only two star which will be start of row and at end of row and on 2 occurances of j matches value of i that’s why j == i is used, i == rows means print (*)pattern values for last row i.e. base of triangle.
On line 16, body of else is there which has printf statement to display blank value whenever if condition fails.
After closing nested for loop, printf statement is added to add new line (\n) to output row to be display on each run. Then main for loop is closed by closing bracket.
In end, return 0 is used as main function is having integer return type.