OverIQ.com

C Program to print various triangular patterns

Last updated on July 27, 2020


Pattern 1: Half Pyramid pattern using * #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * *

The following is a C program to print half pyramid pattern using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**************************************************
 * C Program to print Half Pyramid pattern using *
 **************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Expected Output:

1
2
3
4
5
6
7
Enter number of lines: 5

* 
* * 
* * * 
* * * * 
* * * * *

Pattern 2: Half Pyramid pattern using numbers #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
1     
1     2     
1     2     3     
1     2     3     4     
1     2     3     4     5     
1     2     3     4     5     6     
1     2     3     4     5     6     7     
1     2     3     4     5     6     7     8     
1     2     3     4     5     6     7     8     9     
1     2     3     4     5     6     7     8     9     10

The following is a C program to print half pyramid pattern using numbers:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/********************************************************
 * C Program to print Half Pyramid pattern using numbers
 ********************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
        {
            printf("%-5d ", j);
        }
        printf("\n");
    }

    return 0;
}

Expected Output:

1
2
3
4
5
6
7
Enter number of lines: 5

1     
1     2     
1     2     3     
1     2     3     4     
1     2     3     4     5

Pattern 3: Half pyramid pattern using alphabets #

1
2
3
4
5
6
7
A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G

The following is a C program to print half Pyramid pattern using alphabets:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**********************************************************
 * C Program to print Half Pyramid pattern using alphabets
 **********************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n, ch = 'A';

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = 1; i <= n; i++)
    {        
        // loop to print alphabets
        for(int j = 1; j <= i; j++)
        {
            printf(" %c", ch);
        }

        ch++;

        printf("\n");
    }

    return 0;
}

Expected Output:

1
2
3
4
5
6
7
Enter number of lines: 5

A
B B
C C C
D D D D
E E E E E

Pattern 4: Inverted right triangle pattern using * #

1
2
3
4
5
6
7
8
* * * * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*

The following is a C program to print inverted right triangle using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*****************************************************
 * C Program to print inverted right triangle pattern
******************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = n; i >= 1; i--)
    {        
        // loop to print *
        for(int j = i; j >= 1; j--)
        {
            printf("* ");
        }               

        printf("\n");
    }

    return 0;
}

Expected Output:

1
2
3
4
5
6
7
Enter number of lines: 5

* * * * * 
* * * * 
* * * 
* * 
*

Pattern 5: Full Pyramid pattern using * #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
.
                               * 
                            *  *  * 
                         *  *  *  *  * 
                      *  *  *  *  *  *  * 
                   *  *  *  *  *  *  *  *  * 
                *  *  *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

The following is a C program to print full Pyramid pattern using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*******************************************
 * C Program to print full pyramid using * 
********************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = 1; i <= n; i++)
    {   
        // loop to print leading spaces in each line
        for(int space = 0; space <= n - i; space++)
        {
            printf("   ");
        }

        // loop to print *
        for(int j = 1; j <= i * 2 - 1; j++)
        {
            printf(" * ");
        }               

        printf("\n");
    }

    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Enter number of lines: 8

                         * 
                      *  *  * 
                   *  *  *  *  * 
                *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

Pattern 6: Full inverted pyramid pattern using * #

1
2
3
4
5
6
7
8
*  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  * 
                *  *  *  *  * 
                   *  *  * 
                      *

The following is a C program to print full inverted pyramid pattern using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/***************************************************
 * C Program to print full inverted pyramid using * 
****************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = n; i >= 1; i--)
    {   
        // loop to print leading spaces in each line
        for(int space = n-i; space >= 1; space--)
        {
            printf("   ");
        }

        // loop to print *
        for(int j = i * 2 - 1; j >= 1; j--)
        {
            printf(" * ");
        }               

        printf("\n");
    }

    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Enter number of lines: 10

 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  *  *  * 
                *  *  *  *  *  *  *  *  * 
                   *  *  *  *  *  *  * 
                      *  *  *  *  * 
                         *  *  * 
                            *

Pattern 7: Hollow right-angled triangle using * #

1
2
3
4
5
6
7
8
* 
* * 
*   * 
*     * 
*       * 
*         * 
*           * 
* * * * * * * *

The following is a C program to print hollow right-angled triangle using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**********************************************************
 * C Program to print hollow right angled triangle using *
***********************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for number of lines
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
        {
            //  print * only on the first line, last column of every line and on the last line
            if(j == 1 || j == i || i == n)
            {
                printf("* ");
            }

            else
            {
                printf("  ");    
            }            
        }
        printf("\n");
    }

    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Enter number of lines: 10

* 
* * 
*   * 
*     * 
*       * 
*         * 
*           * 
*             * 
*               * 
* * * * * * * * * *

Pattern 8: Inverted hollow right-angled triangle using * #

1
2
3
4
5
6
7
8
* * * * * * * * 
*           * 
*         * 
*       * 
*     * 
*   * 
* * 
*

The following is a C program to print inverted hollow right-angled triangle using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*******************************************************************
 * C Program to print inverted hollow right angled triangle using *
********************************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for number of lines
    for(int i = n; i >= 1; i--)
    {
        for(int j = i; j >= 1; j--)
        {
            //  print * only on the first line, last line and last column of every line and on the 
            if(j == 1 || j == i || i == n)
            {
                printf("* ");
            }

            else
            {
                printf("  ");    
            }            
        }
        printf("\n");
    }

    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Enter number of lines: 10

* * * * * * * * * * 
*               * 
*             * 
*           * 
*         * 
*       * 
*     * 
*   * 
* * 
*

Pattern 9: Full hollow pyramid using * #

1
2
3
4
5
6
7
8
9
.      
                         * 
                      *     * 
                   *           * 
                *                 * 
             *                       * 
          *                             * 
       *                                   * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

The following is a C program to print a full hollow pyramid using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*************************************************
 * C Program to print full hollow pyramid using * 
*************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = 1; i <= n; i++)
    {   
        // loop to print leading spaces in each line
        for(int space = 0; space <= n - i; space++)
        {
            printf("   ");
        }

        // loop to print *
        for(int j = 1; j <= i * 2 - 1; j++)
        {

            if (j == 1 || (j == i * 2 - 1) || i == n )
            {
                printf(" * ");
            }
            else
            {
                printf("   ");            
            }            
        }               

        printf("\n");
    }

    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Enter number of lines: 10

                               * 
                            *     * 
                         *           * 
                      *                 * 
                   *                       * 
                *                             * 
             *                                   * 
          *                                         * 
       *                                               * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

Pattern 10: Full inverted hollow pyramid using * #

1
2
3
4
5
6
7
8
*  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *                                   * 
       *                             * 
          *                       * 
             *                 * 
                *           * 
                   *     * 
                      *

The following is a C program to print a full hollow inverted pyramid using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**********************************************************
 * C Program to print full inverted hollow pyramid using * 
**********************************************************/

#include<stdio.h> // include stdio.h

int main() {
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for (int i = n; i >= 1; i--) {
        // loop to print leading spaces in each line
        for (int space = n - i; space >= 1; space--) {
            printf("   ");
        }

        // loop to print *
        for (int j = i * 2 - 1; j >= 1; j--) 
        {
            if (j == 1 || (j == i * 2 - 1) || i == n) 
            {
                printf(" * ");
            }             
            else 
            {
                printf("   ");
            }
        }

        printf("\n");
    }

    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Enter number of lines: 10

 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *                                               * 
       *                                         * 
          *                                   * 
             *                             * 
                *                       * 
                   *                 * 
                      *           * 
                         *     * 
                            *

Pattern 11: Diamond pattern using * #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
.
                * 
             *  *  * 
          *  *  *  *  * 
       *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  * 
          *  *  *  *  * 
             *  *  * 
                *

The following is a C program to print a diamond pattern using *:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*********************************************
 * C Program to print Diamond pattern using * 
**********************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop to print upper pyramid
    for(int i = 1; i <= n; i++)
    {   
        // loop to print leading spaces in each line
        for(int space = 0; space <= n - i; space++)
        {
            printf("   ");
        }

        // loop to print *
        for(int j = 1; j <= i * 2 - 1; j++)
        {
            printf(" * ");
        }                              

        printf("\n");
    }

    // loop to print lower pyramid 
    for(int i = n+1; i >= 1; i--)
    {   
        // loop to print leading spaces in each line
        for(int space = n-i; space >= 0; space--)
        {
            printf("   ");
        }

        // loop to print *
        for(int j = i * 2 - 1; j >= 1; j--)
        {
            printf(" * ");
        }               

        printf("\n");
    }

    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Enter number of lines: 8 

                         * 
                      *  *  * 
                   *  *  *  *  * 
                *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  * 
                *  *  *  *  *  *  * 
                   *  *  *  *  * 
                      *  *  * 
                         *

Recommended Programs: