Write a C program to print Christmas tree star pattern using for loop.
#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"); } }
This C example prints the Christmas tree pattern of stars using a while loop.
#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
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * *
* * * *
* * * *
* * * *