geekdoc-python-zh/docs/overiq/140.md

16 KiB

C 程序:打印各种三角形图案

原文:https://overiq.com/c-examples/c-program-to-print-various-triangular-patterns/

最后更新于 2020 年 9 月 24 日


图案 1:使用*的半金字塔图案

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

下面是一个使用*打印半金字塔图案的 C 程序:

/**************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 5

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

模式 2:使用数字的半金字塔模式

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

下面是一个用数字打印半金字塔图案的 C 程序:

/********************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 5

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

模式 3:使用字母的半金字塔模式

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

下面是一个用字母打印半金字塔图案的 C 程序:

/**********************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 5

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

图案 4:使用*的倒直角三角形图案

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

下面是一个使用*打印倒直角三角形的 C 程序:

/*****************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 5

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

模式 5:全金字塔模式使用*

.
                               * 
                            *  *  * 
                         *  *  *  *  * 
                      *  *  *  *  *  *  * 
                   *  *  *  *  *  *  *  *  * 
                *  *  *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

下面是一个使用*打印全金字塔图案的 C 程序:

/*******************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 8

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

图案 6:使用*的全倒金字塔图案

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

下面是一个使用*打印全倒金字塔图案的 C 程序:

/***************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 10

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

图案 7:使用*的空心直角三角形

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

下面是一个使用*打印空心直角三角形的 C 程序:

/**********************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 10

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

图案 8:使用*的倒置空心直角三角形

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

下面是一个使用*打印倒空心直角三角形的 C 程序:

/*******************************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 10

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

图案 9:全空心金字塔使用*

.      
                         * 
                      *     * 
                   *           * 
                *                 * 
             *                       * 
          *                             * 
       *                                   * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

下面是一个使用*打印全空心金字塔的 C 程序:

/*************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 10

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

图案 10:全倒置空心金字塔使用*

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

下面是一个使用*打印全空心倒金字塔的 C 程序:

/**********************************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 10

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

图案 11:菱形图案使用*

.
                * 
             *  *  * 
          *  *  *  *  * 
       *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  * 
          *  *  *  *  * 
             *  *  * 
                *

下面是一个使用*打印钻石图案的 C 程序:

/*********************************************
 * 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;
}

现在试试

预期输出:

Enter number of lines: 8 

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


推荐节目: