C program to print left triangle pattern

In this program, we are going to print left triangle pattern using star/asterisk using c language.
Left triangle pattern looks like below:


Before proceeding with program, you should have basic knowledge of few topics in c that we are going to use for implementing this program.


data types, For loop, variables, input output functions, main method


If you look at left triangle pattern shape, there is gap before star character which gets lesser till last row.
So for implementing this logic we are supposed to add blank space before star pattern so that we can create left triangle shape.
Here we will be using two for loops, parent for loop which we will use to iterate through rows and nested for loop to iterate through columns and print star pattern.

To know more about program execution, follow up program explanation and execution table.


Program Explanation:

Line 1, 2 includes library files such as stdio.h and conio.h which includes necessary functions used in program i.e. input output functions.

Line 3 is start of main function which is entry point for program execution.
Line 5 has variables declared such as I, j which are then used further in for loop for iterating through loop with incrementing/decrementing their values. Variable blankspace is used as loop control variable in for loop where we will print blank value through printf statement.

Variables rows accepts input value entered by user. Line 6 prints message asking user to enter no. of rows. Scanf function is used to store value into variable specified with prefix ampersand operator.

Printf and scanf are input and output functions.

On line 8 is main for loop or outer for loop which has loop control variable I initialized to 1 and condition set to I <= rows means value of I should be lesser than or equal to rows else break out of loop and move to next iteration. Afterwards is the postfix increment of I (I + +). This loop is used to iterate through rows rows-1 times.
Loop structure looks like:

One line 10 is nested for loop used for purpose of printing blankspace over the course of loop execution.
Here loop control variable is initialized to value of I and condition set to blankspace <= rows. This loop is used for iterate through columns rows-1 times. Loop structure looks like:

Inside the body of this loop is printf statement to print blank space by not putting any data inside double quotes.

On line 14 is another nested for loop which is used to iterate through columns and print pattern character unlike the other nested for loop which was used to print only blankspaces.
Here loop control variable is initialized to 1 and condition set to j <= i. j++ does postfix increment of j.
Loop structure looks like:

after closing the nested for loop, we have added printf statement to add new line so that cursor pointer move to next line.

In end is return 0 statement which is used to return 0 value on successful execution of main function.

Leave a Reply

%d bloggers like this: