Creating a Christmas Tree using for loops
Asked Answered
K

8

11

I am trying to make a christmas tree using for loops and nested for loops. For me to do that I need to be able to make a pyramids with *. I have tried countless times and I am having problems making one. Here is my code:

for(int i=1;i<=10;i++){
    for(int j=10;j>i;j--){
        System.out.println(" ");   
    }

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

    for(int l=10;l<=1;l++){
        for(int h=1;h<=10;h++){
            System.out.print(" ");
        }
    }

    System.out.println();  
}

What I am trying to do is:

     *
    ***
   *****
  *******
Klimt answered 9/6, 2015 at 4:50 Comment(1)
write the logic out on a peice of paper.Riggle
M
17

Try this much simpler code:

public class ChristmasTree {

 public static void main(String[] args) {

  for (int i = 0; i < 10; i++) {
   for (int j = 0; j < 10 - i; j++)
    System.out.print(" ");
   for (int k = 0; k < (2 * i + 1); k++)
    System.out.print("*");
   System.out.println();
  }
 }
}

It uses 3 loops:

  • first one for the number of rows,
  • second one for printing the spaces,
  • third one for printing the asterisks.
Mahla answered 9/6, 2015 at 4:58 Comment(6)
Thanks it worked. Could you explain why you did 2*i+1Klimt
First iteration ((2*0)+1) = 1 star. Second iteration ((2*1)+1) = 3 stars. Third iteration ((2*2)+1) = 5 stars and so on.Sanderlin
See for 1 st row there is 1 star.For 2 nd there are 3 , for 3rd there are 5.So it follows the general rule of 2*(n-1)+1. As our i starts form 0 so (n-1)=i. Thus number of stars in (i+1)th row = 2*i+1.Mahla
If I write for (int k = 0; k <= 2 * i; k++) => it also works... how? 2*0 = 0, why does it print one * in the first row for example?Fustanella
@Fustanella because it satisfies the conditions <=0 oncce and the statements inside the loop is executed only once. Therefore one star is printed.Mahla
@SouravKanta underastand (+)Fustanella
W
12

You can do it with simple logic

for (int i = 0; i < 4; i++) 
            System.out.println("   *******".substring(i, 4 + 2*i));
Westley answered 9/6, 2015 at 5:6 Comment(0)
M
2
import java.util.Scanner;

public class cmastree{

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

        int j;
        System.out.println ("Enter a number");
        j=keyboard.nextInt();
        /*take the above part out and change the j variable if you want to set 
        the size*/
        for(int i=1; i<=j; i+=2){
            int numSpaces = (j-i)/2;
        for (int k=0; k<numSpaces; k++){
            System.out.print(" ");
            }
        for(int k=0; k<numSpaces; k++){
            System.out.print("*");
            }
            System.out.println();
        }
    }
}
Mitrewort answered 15/11, 2017 at 3:36 Comment(0)
J
1
public class ChrismasTree {

    public static void main(String[] args) {

        int sizeOfTree = 9;
        double remainderVal = sizeOfTree % 2 ;
        double ans = sizeOfTree / 2 ;

        if (remainderVal == 0) {
            System.out.println("Invalid number enter 9,19 calculat rest yourself u looser ..");
            System.exit(0);
        }
        int middlePos = (int) Math.round(ans + .5);

        for (int i = 0; i <= sizeOfTree; i++) {
            int lStar = middlePos - i;
            int rStar = middlePos + i;

            if (lStar < 1) {
                break;
            }
            printleaves(lStar, rStar, sizeOfTree);
        }
    }

    public static void printleaves(int a,int b, int size){
        System.out.println();
        for (int i = 1; i <= size; i++) {
            if (i > a && i < b ){
                System.out.print("*");
            }else System.out.print(" ");    
        }   
    }
}
Judson answered 22/4, 2018 at 20:38 Comment(0)
B
0
public class Stars {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
       System.out.println("Enter Row/Column Value::");
        int i,j,k,n;
        n=s.nextInt();
        for(i=1; i<n; i++){
            for(j=n+(n/2); j>i; j--){
                System.out.print(" ");}
            for(k=1; k<=2*i-1; k++){
                System.out.print("*");}
            System.out.println("");
            }
         for(i=1; i<n+(n/2); i++){
            for(j=n+(n/2); j>i; j--){
                System.out.print(" ");}
            for(k=1; k<=2*i-1; k++){
                System.out.print("*");}
            System.out.println("");
        }
          for(i=1; i<n-(n/2); i++){
            for(j=n+(n/2); j>1; j--){
                System.out.print(" ");}
            for(k=n/2; k<=(n/2)+1; k++){
                System.out.print("*");}
            System.out.println("");
        }
    }
}
Bonnes answered 13/1, 2017 at 14:6 Comment(2)
above program print Xmas tree.Bonnes
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Knackwurst
S
0
    public class ChristmasTree {
              
    public static void printStars(int number) {
            for (int i = 1; i <= number; i++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    
        public static void printSpaces(int number) {
            for (int i = 0; i < number; i++) {
                System.out.print(" ");
            }
        }
    
        public static void christmasTree(int height) {
            for (int i = 1; i <= height; i++) {
                printSpaces(height - i);
                printStars(i + (i - 1));
            }
         }

 public static void main(String[] args) {
     //     int x = pick some number, but not TOO big )))
          christmasTree(x);
      }
    }
Superorder answered 24/8, 2021 at 18:53 Comment(0)
E
0

You can try something like this

    public class ChristmassTree {

public static void main(String[] args) {
    var height = 5; // Adjust the height of the tree
    printChristmasTree(height);
}

public static void printChristmasTree(int height) {
    for (int i=0; i<height; i++){
        printSpaces(height-i-1);
        printAsterisks(i+1);
        System.out.println();
    }
}

public static void printSpaces(int count) {
    for (int i=0; i<count; i++){
        System.out.print(" ");
    }
}

public static void printAsterisks(int count) {
    for (int i=0; i<count; i++){
        System.out.print("* ");
    }
}}
Edmonton answered 31/8, 2023 at 18:8 Comment(0)
S
-1
def fist(n)
 k=2*n-2
  for i in range(0,n):
   for j in range(0,k):
   k=k-1
    print(end=" ')
   for j in range(0,i+1):
    print("*",end=" ")
   print()
def second(n)
 k=2*n-2
  for i in range(0,n):
   for j in range(0,k):
   k=k-1
   print(end=" ')
   for j in range(0,i+1):
    print("*",end=" ")
   print()
def stem(m)
 k=11
  for i in range(0,5):
   for j in range(0,k):
   print(end=" ")
   for j in range(0,3):
    print("*",end=" ")
   print()
first(7)
second(7)
steam(3)
Sivas answered 10/8, 2019 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.