fork(12) download
  1. #include<stdio.h>
  2.  
  3. int main(){
  4. int i,j,leaves=1,day,rest,branches,max_leaves;
  5. scanf("%d",&day);
  6.  
  7. //Check for exceptions
  8. if(day<=1){
  9. printf("You cannot generate christmas tree");
  10. return 0;
  11. }
  12. else if(day>20){
  13. printf("Tree is no more");
  14. return 0;
  15. }
  16.  
  17. // printf("Heres Your Christmas Tree:\n\n");
  18.  
  19. // For first part
  20. branches=day+1;
  21. max_leaves = 1+(branches-1)*2; // Arithmetic Progression
  22. for(i=1;i<=branches;i++){
  23. for(j=i;j<=max_leaves/2;j++){
  24. printf(" ");
  25. }
  26. for(j=1;j<=leaves;j++){
  27. printf("*");
  28. }
  29. printf("\n");
  30. leaves=leaves+2;
  31. }
  32.  
  33. // For remaining parts
  34. branches=branches-2;
  35. for(rest=1;rest<=day-2;rest++){
  36. leaves=3;
  37. for(i=2;i<=branches+1;i++){
  38. for(j=i;j<=max_leaves/2;j++){
  39. printf(" ");
  40. }
  41. for(j=1;j<=leaves;j++){
  42. printf("*");
  43. }
  44. printf("\n");
  45. leaves=leaves+2;
  46. }
  47. branches--;
  48. }
  49.  
  50. // For stand
  51. for(i=1;i<=2;i++){
  52. for(j=1;j<=max_leaves/2;j++){
  53. printf(" ");
  54. }
  55. printf("*\n");
  56. }
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 4520KB
stdin
5
stdout
     *
    ***
   *****
  *******
 *********
***********
    ***
   *****
  *******
 *********
    ***
   *****
  *******
    ***
   *****
     *
     *