\(\begin{array}{ccccccccc}&&&&*&&&&\\&&&I&*&I&&&\\&&*&*&*&*&*&&\\&I&*&*&*&*&*&I&\\*&*&*&*&*&*&*&*&*\end{array}\)
We want to write a program that displays Christmas trees with candles of different heights.
- All the candles are at the edge of the tree.
- The top of the tree does not have a candle.
- Lines without candles alternate with lines with candles
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
void main()
{
int N, i, j;
do{
printf("Enter N: ");
scanf("%d",&N);
}while(N<=0);
for (i = 0; i < N; i++)
{
// printing the spaces
for (j = 0; j < N-i-1; j++)
printf(" ");
// printing candle or a star
if (i%2)
printf("I");
else
printf("*");
for (j = 0; j < 2*i-1; j++)
printf("*");
if (i%2)
printf("I");
else
if (i>0)
printf("*");
printf("\n");
}
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Star pattern 23