C 语言打印右 Pascal 三角星形图案程序

编写一个 C 程序,使用 while 循环、for 循环和函数打印右 Pascal 三角星形图案,并提供示例。下面的右 Pascal 程序使用嵌套的 for 循环来迭代行并打印星形图案。由于代码存在重复,我们创建了一个单独的 loopLogic 函数来处理逻辑。

#include<stdio.h>
void loopcode(int rows, int i)
{
    for (int j = 0; j < i; j++)
    {
        printf("* ");
    }
    printf("\n");
}
int main(void)  {
    int i, rows;
    
    printf("Enter Rows to Print Right Pascal Triangle =  ");
    scanf("%d", &rows);
    
    for (i = 0; i < rows; i++)
    {
        loopcode(rows, i);
    }
    for (i = rows; i >= 0; i--)
    {
        loopcode(rows, i);
    }
}
C Program to Print Right Pascal Triangle Star Pattern

C 语言使用 while 循环打印右 Pascal 三角星形图案程序

在这个右 Pascal 三角星形图案 程序 中,我们用 while 循环 替换了 for 循环

#include<stdio.h>
void loopcode(int rows, int i)
{
    int j = 0;
    while (j < i)
    {
        printf("* ");
        j++;
    }
    printf("\n");
}
int main(void)  {
    int i, rows;
    
    printf("Enter Rows to Print Right Pascal Triangle =  ");
    scanf("%d", &rows);
    
    i = 0;
    while ( i < rows)
    {
        loopcode(rows, i);
        i++;
    }
    i = rows;
    while ( i >= 0)
    {
        loopcode(rows, i);
        i--;
    }
}

输出

Enter Rows to Print Right Pascal Triangle =  11

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

在下面的右 Pascal 三角星形图案 程序 中,用户定义的 函数 接受行数和字符,以使用符号打印图案。

#include<stdio.h>
void loopcode(int rows, int i, char ch)
{
    for (int j = 0; j < i; j++)
    {
        printf("%c ",ch);
    }
    printf("\n");
}
int main(void)  {
    int i, rows;
    char ch;
    
    printf("Enter Character = ");
    scanf("%c", &ch);
    
    printf("Enter Rows to Print Right Pascal Triangle =  ");
    scanf("%d", &rows);
    
    for (i = 0; i < rows; i++)
    {
        loopcode(rows, i, ch);
    }
    for (i = rows; i >= 0; i--)
    {
        loopcode(rows, i,  ch);
    }
}

输出

Enter Character = $
Enter Rows to Print Right Pascal Triangle =  15

$ 
$ $ 
$ $ $ 
$ $ $ $ 
$ $ $ $ $ 
$ $ $ $ $ $ 
$ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ 
$ $ $ $ $ $ 
$ $ $ $ $ 
$ $ $ $ 
$ $ $ 
$ $ 
$