Advertisement
tripTiPscout

Christmas Tree Recursive

Nov 14th, 2022
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package Training;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.  
  12.         pattern(n, n);
  13.  
  14.     }
  15.  
  16.     static void printSpace(int n) {
  17.  
  18.         if (n == 0) {
  19.             return;
  20.         }
  21.         System.out.print(" ");
  22.  
  23.         printSpace(n - 1);
  24.     }
  25.  
  26.  
  27.     static void printInsideSpaces(int n) {
  28.  
  29.         if (n <= 0) {
  30.  
  31.             if (n == 0) {
  32.                 System.out.println("*");
  33.             } else {
  34.                 System.out.println();
  35.             }
  36.  
  37.             return;
  38.         }
  39.  
  40.         System.out.print(" ");
  41.  
  42.         printInsideSpaces(n - 1);
  43.     }
  44.  
  45.  
  46.     static void pattern(int n1, int n2) {
  47.  
  48.         if (n1 == 0) {
  49.             return;
  50.         }
  51.  
  52.         printSpace(n1 - 1);
  53.  
  54.         System.out.print("*");
  55.  
  56.         printInsideSpaces(((n2 - n1) * 2) - 1);
  57.  
  58.         pattern(n1 - 1, n2);
  59.     }
  60.  
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement