Created
January 10, 2022 10:50
-
-
Save SiddharthManthan/3b51551e23eadc2c5c7d1877200b1936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Patterns { | |
public static void main(String[] args) { | |
int n = 5; | |
for (int i = 0; i <= n; i++) { | |
for (int space=n-i; space >= 1; space--) { | |
System.out.print(" "); | |
} | |
printNumbers(0, i); | |
} | |
for (int i = n-1; i >= 0; i--) { | |
for (int space = 1; space <= (n - i); space++) { | |
System.out.print(" "); | |
} | |
printNumbers(0, i); | |
} | |
System.out.println(); | |
for(int i=1; i<=n; i++) { | |
for(int space=n-i; space>=1; space--) { | |
System.out.print(" "); | |
} | |
printNumberWithSpace(i, i); | |
} | |
System.out.println(); | |
for(int i=1; i<=n; i++) { | |
for(int space=n-i; space>=1; space--) { | |
System.out.print(" "); | |
} | |
printStarWithSpace(i); | |
} | |
for(int i=n-1; i>=1; i--) { | |
for(int space=1; space<=n-i; space++) { | |
System.out.print(" "); | |
} | |
printStarWithSpace(i); | |
} | |
} | |
public static void printNumbers(int start, int end) { | |
for(int i=start; i<=end; i++) { | |
System.out.print(i); | |
} | |
for(int i=(end - 1); i>=start; i--) { | |
System.out.print(i); | |
} | |
System.out.println(); | |
} | |
public static void printNumberWithSpace(int number, int times) { | |
for(int i=1; i<=times; i++) { | |
System.out.print(number+" "); | |
} | |
System.out.println(); | |
} | |
public static void printStarWithSpace(int times) { | |
for(int i=1; i<=times; i++) { | |
System.out.print("* "); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment