Top Down Right Triangle Printing In C



A triangle with one of its angle 90° is called right triangle. We shall now see how to print stars *, in right triangle shape, but upside down on x axis.

Algorithm

Algorithm should look like this −

Step 1 - Take number of rows to be printed, n.
Step 2 - Make outer iteration I from N to 1 times to print rows
Step 3 - Make inner iteration for J to I
Step 3 - Print "*" (star)
Step 4 - Print NEWLINE character after each inner iteration
Step 5 - Return

Pseudocode

We can derive a pseudocode for the above mentioned algorithm, as follows −

procedure topdownright_triangle

   FOR I = N to 1 DO
      FOR J = 1 to I DO
         PRINT "*" 
      END FOR
      PRINT NEWLINE
   END FOR
   
end procedure

Implementation

Implementation of right triangle in C is as follows −

#include <stdio.h>

int main() {
   int n, i, j;

   n = 5;

   for(i = n; i >= 1; i--) {
      for(j = 1; j <= i; j++)
         printf("* ");

      printf("\n");
   }
   
   return 0;
}

The output should look like this −

* * * * *
* * * *
* * *
* *
*
patterns_examples_in_c.htm
Advertisements