Advance Patterns By Samrat Sir Strategy
unknown
java
a year ago
6.5 kB
5
Indexable
public class Advance {
public static void printPattern(int n) {
int total_no_of_lines = n;
int stars = 1;
int spaces = n - 1;
int current_no_of_line = 1;
while (current_no_of_line <= total_no_of_lines) {
// print spaces
for (int i = 1; i <= spaces; i++) {
System.out.print(" ");
}
// print stars
for (int i = 1; i <= stars; i++) {
System.out.print("*");
}
// prepare for next line
System.out.println();
current_no_of_line++;
stars++;
spaces--;
}
}
public static void butterflyPattern(int n) {
int total_no_of_lines = 2 * n;
int stars = 1;
int spaces = 2 * n - 2;
int current_number_of_line = 1;
while (current_number_of_line <= total_no_of_lines) {
// stars
for (int i = 1; i <= stars; i++) {
System.out.print("* ");
}
// spaces
for (int i = 0; i <= spaces; i++) {
System.out.print(" ");
}
// stars
for (int i = 1; i <= stars; i++) {
System.out.print("* ");
}
// prepare for next line
System.out.println();
if (current_number_of_line < n) {
stars++;
spaces = spaces - 2;
} else if (current_number_of_line == n) {
// do not do anything
} else {
stars--;
spaces = spaces + 2;
}
current_number_of_line++;
}
}
public static void numberPyramid(int n) {
int total_no_of_lines = n;
int stars = 1;
int spaces = n - 1;
int current_no_of_line = 1;
while (current_no_of_line <= total_no_of_lines) {
// print spaces
for (int i = 1; i <= spaces; i++) {
System.out.print(" ");
}
// print stars
for (int i = 1; i <= stars; i++) {
System.out.print(i + " ");
}
// prepare for next line
System.out.println();
current_no_of_line++;
stars++;
spaces--;
}
}
public static void hollowRectangle(int row, int col) {
for (int r = 1; r <= row; r++) {
for (int c = 1; c <= col; c++) {
if (c == 1 || r == 1 || r == row || c == col) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void solidRhombus(int n) {
int total_no_of_lines = n;
int stars = n;
int spaces = n - 1;
int current_no_of_line = 1;
while (current_no_of_line <= total_no_of_lines) {
// spaces
for (int i = 1; i <= spaces; i++) {
System.out.print(" ");
}
// stars
for (int i = 1; i <= stars; i++) {
System.out.print("*");
}
// prepare for next line
System.out.println();
current_no_of_line++;
spaces--;
}
}
public static void hollowRhombus(int n) {
int total_no_of_lines = n;
int stars = n;
int spaces = n - 1;
int current_no_of_line = 1;
while (current_no_of_line <= total_no_of_lines) {
// spaces
for (int i = 1; i <= spaces; i++) {
System.out.print(" ");
}
// stars
for (int i = 1; i <= stars; i++) {
if (current_no_of_line == 1 || current_no_of_line == n) {
System.out.print("*");
} else {
if (i == 1 || i == stars) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
}
// prepare for next line
current_no_of_line++;
System.out.println();
spaces--;
}
}
public static void diamond(int n) {
int total_no_of_lines = 2 * n;
int stars = 1;
int spaces = n - 1;
int current_no_of_line = 1;
while (current_no_of_line <= total_no_of_lines) {
// spaces
for (int i = 1; i <= spaces; i++) {
System.out.print(" ");
}
// stars
for (int i = 1; i <= stars; i++) {
System.out.print("*");
}
// prepare for next line
System.out.println();
current_no_of_line++;
if (current_no_of_line < n) {
stars = stars + 2;
spaces--;
} else if (current_no_of_line == n) {
}
else {
stars = stars - 2;
spaces++;
}
}
}
public static void pallindrome(int n) {
int total_no_of_lines = n;
int stars = 1;
int spaces = n - 1;
int current_no_of_line = 1;
while (current_no_of_line <= total_no_of_lines) {
// spaces
for (int i = 1; i <= spaces; i++) {
System.out.print(" ");
}
// stars
int numbertoprint = 1;
for (int i = 1; i <= stars; i++) {
System.out.print(numbertoprint + " ");
if (i <= stars / 2) {
numbertoprint++;
} else {
numbertoprint--;
}
}
// prepare for next line
System.out.println();
current_no_of_line++;
stars = stars + 2;
spaces--;
}
}
public static void main(String[] args) {
int n = 5;
// int rows = 4;
// int cols = 10;
hollowRhombus(n);
// hollowRectangle(rows, cols);
}
}
Editor is loading...
Leave a Comment