Program to Print Christmas Tree Star Pattern
Java christmas tree: In this article we are going to see how to print the Christmas tree pattern.
Example-1 When height Of Tree value=10 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | | | | | | | | | | | | | | | | | |
Example-2: When height Of Tree value=5 * * * * * * * * * * * * * * * | | | | | | | |
Now, let’s see the actual program to print it.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Approach:
- Enter the height of the Christmas tree in an integer variable
heightOfTree
. - Take first for loop to print the full height of the tree.
- Take second/inner for loop to print the column values (i.e. stars and spaces).
- Then use another for loop for the lower part of the tree.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Height Of The Christmas Tree : "); //Taking height Of Tree as input from user int heightOfTree=scan.nextInt(); //Outer loop for the upper part of the tree for(int r = 1;r<=heightOfTree;r++) { for(int c = heightOfTree-r; c > 0; c--) //Inner loop to print the spaces System.out.print(" "); for(int d = 1;d<=r;d++) //Inner loop to print the stars System.out.print("* "); //Prints a new line System.out.println(""); } //Second outer loop to print the lower part of the tree for(int r = 1; r<=heightOfTree-1;r++) { System.out.print(" "); for(int c = heightOfTree -3;c>0;c--) //Inner loop that prints spaces System.out.print(" "); for(int d = 2;d>0;d--) //Inner loop that prints the straight lines System.out.print("| "); //Prints a new line System.out.println(); } } }
Method-2 : User Input Character
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Height Of The Christmas Tree : "); //Taking height Of Tree as input from user int heightOfTree=scan.nextInt(); System.out.print("Enter any character : "); //Taking height Of Tree as input from user char ch=scan.next().charAt(0); //Outer loop for the upper part of the tree for(int r = 1;r<=heightOfTree;r++) { for(int c = heightOfTree-r; c > 0; c--) //Inner loop to print the spaces System.out.print(" "); for(int d = 1;d<=r;d++) //Inner loop to print the stars System.out.print(ch+" "); //Prints a new line System.out.println(""); } //Second outer loop to print the lower part of the tree for(int r = 1; r<=heightOfTree-1;r++) { System.out.print(" "); for(int c = heightOfTree -3;c>0;c--) //Inner loop that prints spaces System.out.print(" "); for(int d = 2;d>0;d--) //Inner loop that prints the straight lines System.out.print("| "); //Prints a new line System.out.println(); } } }
Output- Height Of The Christmas Tree : 10 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ | | | | | | | | | | | | | | | | | |
C Code:
#include <stdio.h> int main(int argc, char const *argv[]) { printf("Height Of The Christmas Tree : "); //Taking height Of Tree as input from user int heightOfTree; scanf("%d", &heightOfTree); //Outer loop for the upper part of the tree for (int r = 1; r <= heightOfTree; r++) { for (int c = heightOfTree - r; c > 0; c--) //Inner loop to print the spaces printf(" "); for (int d = 1; d <= r; d++) //Inner loop to print the stars printf("* "); //Prints a new line printf("\n"); } //Second outer loop to print the lower part of the tree for (int r = 1; r <= heightOfTree - 1; r++) { printf(" "); for (int c = heightOfTree - 3; c > 0; c--) //Inner loop that prints spaces printf(" "); for (int d = 2; d > 0; d--) //Inner loop that prints the straight lines printf("| "); //Prints a new line printf("\n"); } return 0; }
Output- Height Of The Christmas Tree : 10 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | | | | | | | | | | | | | | | | | |
C++ Code:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "Height Of The Christmas Tree : "; //Taking height Of Tree as input from user int heightOfTree; cin >> heightOfTree; //Outer loop for the upper part of the tree for (int r = 1; r <= heightOfTree; r++) { for (int c = heightOfTree - r; c > 0; c--) //Inner loop to print the spaces cout << " "; for (int d = 1; d <= r; d++) //Inner loop to print the stars cout << "* "; //Prints a new line printf("\n"); } //Second outer loop to print the lower part of the tree for (int r = 1; r <= heightOfTree - 1; r++) { cout << " "; for (int c = heightOfTree - 3; c > 0; c--) //Inner loop that prints spaces cout << " "; for (int d = 2; d > 0; d--) //Inner loop that prints the straight lines cout << "| "; //Prints a new line cout << endl; } return 0; }
Output- Height Of The Christmas Tree : 10 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | | | | | | | | | | | | | | | | | |
Related Java Star Pattern Programs: