Upside Down Triangle Printing In C


Advertisements

A triangle with all sides equal is called equilateral triangle. We shall now see how to print stars *, in equilateral triangle shape, but upside-down.

Algorithm

Algorithm should look like this −

Step 1 - Take number of rows to be printed, n.
Step 2 - Make an iteration for n times
Step 3 - Print " " (space) for in decreasing order from 1 to n-1
Step 4 - Print "* " (start, space) in increasing order from 1 to I
Step 5 - Return

Pseudocode

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

procedure upsidedown_triangle

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

Implementation

Implementation of equilateral triangle upside-down in C is as follows −

#include <stdio.h>

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

   n = 5;

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

      for(j = i; j <= n; j++)
         printf("* ");

      printf("\n");
   }

   return 1;
}

The output should look like this −

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