Christmas Tree Pattern Program

7082
Christmas Tree Pattern Program in Java
Advertisement

Christmas Tree Pattern Program

Christmas is the festival of lights and gifts. It always comes to give happiness to everyone’s life. so why not you can try to print the Christmas Tree Program in Java.

We are here providing two Christmas Tree Pattern Programs, select your own choice and print the Christmas tree and celebrate Christmas.

Christmas Tree Pattern Program also called as Xmas Pattern Program.

Christmas Tree Program 1:

Java
         * 
        * * 
      * * * 
     * * * * 
    * * * * * 
  * * * * * * 
 * * * * * * * 
* * * * * * * * 
        | | 
        | | 
        | | 
        | | 
        | | 
        | | 
        | |

Program 1:

Java
import java.util.Scanner;

class xmax_pattern_1{

  public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the height of tree : ");
    int h = sc.nextInt();

    //beginning of upper part
    for(int i = 1;i <= h;i++){

      for(int j = h-i;j > 0;j--){
        System.out.print(" ");
      }

      for(int k = 1;k <= i;k++){
        System.out.print("* ");
      }

      System.out.println();
    }//end of upper part

    //beginning of lower part
    for(int i = 1;i <= h-1;i++){

      System.out.print(" ");

      for(int j = h-3;j > 0;j--){
        System.out.print(" ");
      }

      for(int k = 2;k > 0;k--){
        System.out.print("| ");
      }

      System.out.println();
    }// end of lower part
  }
}

Output:

Output
Enter the height of tree : 
8
         * 
        * * 
      * * * 
     * * * * 
    * * * * * 
  * * * * * * 
 * * * * * * * 
* * * * * * * * 
        | | 
        | | 
        | | 
        | | 
        | | 
        | | 
        | |

Christmas Tree Program 2:

Java
                       *
                      * * 
                    * * * 
                   * * * * 
                    * * * 
                   * * * * 
                 * * * * * 
                * * * * * * 
                 * * * * * 
                * * * * * * 
              * * * * * * * 
             * * * * * * * * 
               * * * * * * * 
             * * * * * * * * 
            * * * * * * * * * 
           * * * * * * * * * * 
                  * * * * 
                  * * * * 
                  * * * * 
                  * * * * 

Program 2:

Java
import java.util.Scanner;

class xmax_pattern_2{

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the height of the Tree : ");

        int h = sc.nextInt();

        System.out.println("Enter the width of the Tree : ");

        int w = sc.nextInt();

        int space = w*5;

        int x = 1;

        for(int a = 1;a <= h;a++){

            for(int i = x;i <= w;i++){

                for(int j = space;j >= i;j--){

                    System.out.print(" ");
                }

                for(int k = 1;k <= i;k++){

                    System.out.print("* ");
                }

                System.out.println();
            }

            x = x+2;
            w = w+2;
        }

        for(int i = 1;i <= 4;i++){

            for(int j = space-3;j >= 1;j--){

                System.out.print(" ");
            }

            for(int k= 1;k <= 4;k++){
                System.out.print("* ");
            }

            System.out.println();
        }
    }
}

Output:

Java
Enter the height of the Tree : 
4
Enter the width of the Tree : 
4
                       *
                      * * 
                    * * * 
                   * * * * 
                    * * * 
                   * * * * 
                 * * * * * 
                * * * * * * 
                 * * * * * 
                * * * * * * 
              * * * * * * * 
             * * * * * * * * 
               * * * * * * * 
             * * * * * * * * 
            * * * * * * * * * 
           * * * * * * * * * * 
                  * * * * 
                  * * * * 
                  * * * * 
                  * * * * 

These pattern programs come to help you building your analytical skill in Java programming so keep testing and keep coding the pattern programs available here.

I hope you will find these Pattern Programs very helpful for your preparation. If you have doubts regarding these programs comment below or directly mail us. We will be happy to help you.

🎅Merry Christmas🎅

If you need more Pattern Programs for Practice. Please follow the Link: Pattern Programs

Also, find below links useful:

Arrays in Java

Different ways to find the length of the array

Linear search in Java