C program to print reversed left triangle pattern

In this program, we are going to print reversed left triangle pattern using asterisk or star special character in c. This program is similar to reversed left hollow triangle pattern program, but only difference is that hollow pattern print blank spaces in middle of reversed left triangle shape.

Reversed left triangle pattern looks as shown below:

Pre-requisites:

To able to write this program, knowledge of concepts like data types, variables, input output functions, for loop, main method is necessary.

Approach:

Looking at shape as shown above, we can conclude that from first row onwards blank space starts replacing star special character in incremental order (one blank space on 2nd row, 2 blank space on 3rd row and so on).

So here we are supposed to add blank space in incremental order and then add star character after that blank position so desired pattern is displayed.

This program makes use of three for loops:

  • Parent for loop/main loop
  • Nested for loop/inner loop
  • 2nd nested for loop/inner loop

Variable I and j are used as loop control variables. Variable rows is used to store user input which is then used as rows limit in program. Parent for loop is used for rotating through rows and both nested for loops are used for rotating through columns. Purpose of using two nested for loop is that one is used for printing blank values and one is used for printing star special character.

Iterations:

Parent for loop structure looks like: for(i=0;iProgram:

Output

Program explanation:

Line 1 and 2 declares header files stdio.h and conio.h. These header files includes predefined functions which are used in program.
Line 3 is main function with integer return type which is entry point into program execution.

Line 5 declares the loop control variables I,j,blankspace and variables rows to accepts user input.
Line 6 has printf statement which prints message which asks user to input no. of rows which will be used to display output over that many rows. printf(“Enter the no. of rows:\n”);

Line 7 is scanf statement used which reads user input.

Here 3 for loops are used:

  • Parent for loop – to iterate through rows from 0 to maximum limit – 1 i.e. rows – 1
    Loop structure: for(i=0;i
  • Nested for loop – this nested for loop is used for iterating through columns and printing blank values wherever required. This loop iterates from 1 to less than or equal to current parent loop iteration i.e. 1 to i.
    for(blankspace=1;blankspace<=i;blankspace++)
  • 2nd nested for loop – this nested for loop is used for iterating through columns and printing star pattern special character whererver required. This loop iterates form I to less than or equal to rows with I having dynamic value after each iteration (value of I can change from 1 to 5).
    for(j=i;j<=rows;j++)

Line 10 i.e. body of first nested for loop contains printf statement to print blank value. This statement gets executed only when body of for loop gets executed upon evaluate condition is true.

Line 14 i.e. second nested for loop body consists of printf statement to print star special character pattern which gets executed when evaluate condition is true

After nested for loop is closed, next is printf statement to add new line.
On line 20, return 0 statement returns value 0 on successful completion of program execution. O indicates successful.

Leave a Reply

%d bloggers like this: