C program to print door shape using star pattern

In this program, we will print door shape using star or asterisk pattern in c. Door pattern looks as shown below:

Pre-requisites: To write this program, concepts like for loop, variables, data types, input output function, if else statement is necessary.

Approach:

As we can see, here we have printed shape where there are only 3 sides connected and bottom is left blank.
So that means we will not print asterisk pattern on last row (bottom of shape) and print asterisk pattern on first row, first column and last column.
By doing this we will achieve door pattern shape.

In the source code for this program, we will use two for loops. First is parent for loop and second one is nested for loop or inner loop.
Using parent/main for loop we are going to iterate through rows and using nested for loop we are going to iterate through columns and print asterisk pattern based on condition applied inside for loop.

Counter variable i is used to iterate through rows and we are going to print asterisk on first row so our first condition will be I is equal to 1 i.e. i == 1.
Counter variable j is used to iterate through columns and we are going to print star pattern on first column and last column so that our condition will be j is equal to 1 and j is equal to n (here n is input number which will be used as limit) i.e. (j==1 && j==n).

And we are supposed to leave last row blank i.e. the bottom of shape so that we need to print blank space on last row. Printf() statement function will be used to print values.

Program:

Output:

Leave a Reply

%d bloggers like this: