编写一个C程序,使用for循环打印8星图案。
#include <stdio.h>
int main()
{
int rows;
printf("Please Enter 8 Star Pattern Rows = ");
scanf("%d", &rows);
printf("Printing 8 Pattern of Stars\n");
for (int i = 1; i <= rows * 2 - 1; i++)
{
if (i == 1 || i == rows || i == rows * 2 - 1)
{
for (int j = 1; j <= rows; j++)
{
if (j == 1 || j == rows)
{
printf(" ");
}
else
{
printf("*");
}
}
}
else
{
for (int k = 1; k <= rows; k++)
{
if (k == 1 || k == rows)
{
printf("*");
}
else
{
printf(" ");
}
}
}
printf("\n");
}
}

C语言使用while循环打印8星图案的程序
#include <stdio.h>
int main()
{
int i, j, k, rows;
printf("Please Enter 8 Star Pattern Rows = ");
scanf("%d", &rows);
printf("\n");
i = 1;
while (i <= rows * 2 - 1)
{
if (i == 1 || i == rows || i == rows * 2 - 1)
{
j = 1;
while (j <= rows)
{
if (j == 1 || j == rows)
{
printf(" ");
}
else
{
printf("*");
}
j++;
}
}
else
{
k = 1;
while (k <= rows)
{
if (k == 1 || k == rows)
{
printf("*");
}
else
{
printf(" ");
}
k++;
}
}
printf("\n");
i++;
}
}
Please Enter 8 Star Pattern Rows = 10
********
* *
* *
* *
* *
* *
* *
* *
* *
********
* *
* *
* *
* *
* *
* *
* *
* *
********
本C程序使用do while循环打印8位数字星形图案。
#include <stdio.h>
int main()
{
int i, j, k, rows;
printf("Please Rows = ");
scanf("%d", &rows);
printf("\n");
i = 1;
do
{
if (i == 1 || i == rows || i == rows * 2 - 1)
{
j = 1;
do
{
if (j == 1 || j == rows)
{
printf(" ");
}
else
{
printf("*");
}
} while (++j <= rows);
}
else
{
k = 1;
do
{
if (k == 1 || k == rows)
{
printf("*");
}
else
{
printf(" ");
}
} while (++k <= rows);
}
printf("\n");
} while (++i <= rows * 2 - 1);
}
Please Rows = 12
**********
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**********
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**********
在此C示例中,printingPattern8函数允许行和字符,并打印给定字符的8位图案。
#include <stdio.h>
void printingPattern8(int rows, char ch);
int main()
{
int rows;
char ch;
printf("Enter Character for 8 Pattern = ");
scanf("%c", &ch);
printf("Please Enter 8 Pattern Rows = ");
scanf("%d", &rows);
printf("Printing 8 Pattern of Stars\n");
printingPattern8(rows, ch);
}
void printingPattern8(int rows, char ch)
{
for (int i = 1; i <= rows * 2 - 1; i++)
{
if (i == 1 || i == rows || i == rows * 2 - 1)
{
for (int j = 1; j <= rows; j++)
{
if (j == 1 || j == rows)
{
printf(" ");
}
else
{
printf("%c", ch);
}
}
}
else
{
for (int k = 1; k <= rows; k++)
{
if (k == 1 || k == rows)
{
printf("%c", ch);
}
else
{
printf(" ");
}
}
}
printf("\n");
}
}
Enter Character for 8 Pattern = ^
Please Enter 8 Pattern Rows = 14
Printing 8 Pattern of Stars
^^^^^^^^^^^^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^^^^^^^^^^^^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^^^^^^^^^^^^