打印圣诞树明星图案的C语言程序

104 阅读1分钟

编写一个C语言程序,使用for循环打印圣诞树的星星图案。

#include <stdio.h>

int main() { int width, height, space, i, j, k, n = 1;

printf("Please Enter Chirstmas Tree Width & Height = ");
scanf("%d %d", &width, &height);

space = width \* height;

printf("Printing Chirstmas Tree Pattern of Stars\\n");

for (int x = 1; x <= height; x++)
{
	for (i = n; i <= width; i++)
	{
		for (j = space; j >= i; j--)
		{
			printf(" ");
		}
		for (k = 1; k <= i; k++)
		{
			printf("\* ");
		}
		printf("\\n");
	}
	n = n + 2;
	width = width + 2;
}
for (i = 1; i <= height - 1; i++)
{
	for (j = space - 3; j >= 0; j--)
	{
		printf(" ");
	}
	for (k = 1; k <= height - 1; k++)
	{
		printf("\* ");
	}
	printf("\\n");
}

image.png

这个C例子使用while循环打印圣诞树图案的星星。

#include <stdio.h>

void ChirstmasTreePattern(int width, int height, char ch);

int main()
{
	int width, height, i, j, k, n = 1;
	char ch;

	printf("\nEnter Character for Chirstmas Tree Pattern = ");
	scanf("%c", &ch);
	
	printf("Please Enter Chirstmas Tree Width & Height = ");
	scanf("%d %d", &width, &height);
		
	int space = width * height;
		
	printf("Printing Chirstmas Tree Pattern of Stars\n");
	ChirstmasTreePattern(width, height, ch);	
}

void ChirstmasTreePattern(int width, int height, char ch)
{
	int space = width * height;
	int i, j, k, n = 1;
		
	for (int x = 1; x <= height; x++ ) 
	{
		for (i = n; i <= width; i++ )
		{
			for(j = space; j >= i; j--)
			{
				printf(" ");
			}
			for(k = 1; k <= i; k++)
			{
				printf("%c ", ch);
			}
			printf("\n");
		}
		n = n + 2;
		width = width + 2;		
	}
	for(i = 1; i <= height - 1; i++)
	{
		for(j = space - 3; j >= 0; j--)
		{
			printf(" ");
		}
		for(k = 1; k <= height - 1; k++)
		{
			printf("%c ", ch);
		}
		printf("\n");
	}	
}
Enter Character for Chirstmas Tree Pattern = *
Please Enter Chirstmas Tree Width & Height = 8 5
Printing Chirstmas Tree Pattern of Stars
                                        * 
                                       * * 
                                      * * * 
                                     * * * * 
                                    * * * * * 
                                   * * * * * * 
                                  * * * * * * * 
                                 * * * * * * * * 
                                      * * * 
                                     * * * * 
                                    * * * * * 
                                   * * * * * * 
                                  * * * * * * * 
                                 * * * * * * * * 
                                * * * * * * * * * 
                               * * * * * * * * * * 
                                    * * * * * 
                                   * * * * * * 
                                  * * * * * * * 
                                 * * * * * * * * 
                                * * * * * * * * * 
                               * * * * * * * * * * 
                              * * * * * * * * * * * 
                             * * * * * * * * * * * * 
                                  * * * * * * * 
                                 * * * * * * * * 
                                * * * * * * * * * 
                               * * * * * * * * * * 
                              * * * * * * * * * * * 
                             * * * * * * * * * * * * 
                            * * * * * * * * * * * * * 
                           * * * * * * * * * * * * * * 
                                * * * * * * * * * 
                               * * * * * * * * * * 
                              * * * * * * * * * * * 
                             * * * * * * * * * * * * 
                            * * * * * * * * * * * * * 
                           * * * * * * * * * * * * * * 
                          * * * * * * * * * * * * * * * 
                         * * * * * * * * * * * * * * * * 
                                      * * * * 
                                      * * * * 
                                      * * * * 
                                      * * * *