C 语言打印字母 C 形状的程序

在本文中,我们将展示如何使用 for 循环、while 循环和函数编写一个 C 语言程序来打印字母大写 C 形状或星形图案。

#include <stdio.h>

int main() {
int rows;

printf("Enter Rows = ");
scanf("%d", &rows);

for (int i = 0; i < rows; i++)
{
if (i > 0 && i < rows - 1)
{
printf("*");
}
for (int j = 0; j < rows / 2 + 1; j++)
{
if (i == 0 || i == rows - 1)
{
printf(" *");
}
}
printf("\n");
}
}
Enter Rows = 15
 * * * * * * * *
*
*
*
*
*
*
*
*
*
*
*
*
*
 * * * * * * * *

这个星形字母 C 图案程序使用 while 循环 来迭代行和列,而不是使用 for 循环。有关更多星形图案程序,请点击此处

#include <stdio.h>

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

printf("Enter Rows = ");
scanf("%d",&rows);

i = 0 ;
while (i < rows )
{
printf("*");
j = 0 ;
while ( j < rows - 1 )
{
if (i == 0 || i == rows - 1)
{
printf("*");
}
else
{
printf(" ");
}
j++;
}
printf("\n");
i++;
}

return 0;
}
Enter Rows = 16
****************
*               
*               
*               
*               
*               
*               
*               
*               
*               
*               
*               
*               
*               
*               
****************

在此 C 程序中,我们创建了一个 alphabetCPat 函数,该函数接受这些值并打印给定字符的字母 C 图案。

#include <stdio.h>

void alphabetCPat(int rows, char a)
{
for (int i = 0; i < rows; i++)
{
if (i > 0 && i < rows - 1)
{
printf("%c", a);
}

for (int j = 0; j < rows / 2 + 1; j++)
{
if (i == 0 || i == rows - 1)
{
printf(" %c", a);
}

}
printf("\n");
}
}

int main()
{
int rows;
char a;

printf("Enter Alphabet = ");
scanf("%c", &a);

printf("Enter Rows = ");
scanf("%d",&rows);

alphabetCPat(rows, a);

return 0;
}
Program to Print Alphabet C Pattern