Constructing a triangles using characters and nested for loop

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Constructing a triangles using characters and nested for loop in C programming

To show: How to use the nested for loops in the creative ways in C programming

 

 

/* -----for loop, the first triangle-------*/

#include <stdio.h>

 

int main()

{

int i, j, k, l;

 

printf("Triangle lol!\n");

/* first for loop, set the rows... */

for(i=15; i>=0; i--)

{

/* second for loop, set the space... */

for(j=15; j>=1+i; j--)

printf(" ");

 

/* third for loop, print the characters... */

for(j=1; j<=2*i+1; j++)

/* print the character... */

printf("H");

/* go to new line... */

printf("\n");

}

 

/* Another inverse triangle */

for(k=1; k <= 16; k++)

{

for(l=1; l<=16-k; l++)

printf(" ");

for(l=1; l<=2*k-1; l++)

printf("T");

printf("\n");

}

return 0;

}

 

Output example:

 

Triangle lol!

HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

  HHHHHHHHHHHHHHHHHHHHHHHHHHHHH

   HHHHHHHHHHHHHHHHHHHHHHHHHHH

    HHHHHHHHHHHHHHHHHHHHHHHHH

     HHHHHHHHHHHHHHHHHHHHHHH

      HHHHHHHHHHHHHHHHHHHHH

       HHHHHHHHHHHHHHHHHHH

        HHHHHHHHHHHHHHHHH

         HHHHHHHHHHHHHHH

          HHHHHHHHHHHHH

           HHHHHHHHHHH

            HHHHHHHHH

             HHHHHHH

              HHHHH

               HHH

                H

                T

              TTT

             TTTTT

            TTTTTTT

           TTTTTTTTT

          TTTTTTTTTTT

         TTTTTTTTTTTTT

        TTTTTTTTTTTTTTT

       TTTTTTTTTTTTTTTTT

      TTTTTTTTTTTTTTTTTTT

     TTTTTTTTTTTTTTTTTTTTT

    TTTTTTTTTTTTTTTTTTTTTTT

   TTTTTTTTTTTTTTTTTTTTTTTTT

  TTTTTTTTTTTTTTTTTTTTTTTTTTT

 TTTTTTTTTTTTTTTTTTTTTTTTTTTTT

TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index