0% found this document useful (0 votes)
17 views13 pages

Pattern Based Question With Solution

Uploaded by

kingamaan460
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

Pattern Based Question With Solution

Uploaded by

kingamaan460
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1 — Butterfly Pattern

Input: n = 4
Output:
* *
** **
*** ***
********
*** ***
** **
* *

public class Butterfly {


public static void main(String[] args) {
int n = 4;
// Upper half
for (int i = 1; i <= n; i++) {
// left stars
for (int j = 1; j <= i; j++) System.out.print("*");
// spaces
for (int j = 1; j <= 2*(n - i); j++) System.out.print(" ");
// right stars
for (int j = 1; j <= i; j++) System.out.print("*");
System.out.println();
}
// Lower half
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) System.out.print("*");
for (int j = 1; j <= 2*(n - i); j++) System.out.print(" ");
for (int j = 1; j <= i; j++) System.out.print("*");
System.out.println();
}
}
}

2 — Concentric Rectangular Number Pattern (Square of numbers)


Input: n = 4 (produces 7×7)
Output:
4444444
4333334
4322234
4321234
4322234
4333334
4444444

public class ConcentricRectangular {


public static void main(String[] args) {
int n = 4;
int size = 2*n - 1;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int top = i;
int left = j;
int right = size - 1 - j;
int bottom = size - 1 - i;
int val = Math.min(Math.min(top, bottom), Math.min(left, right));
System.out.print((n - val) + (j == size-1 ? "" : " "));
}
System.out.println();
}
}
}
3 — Zig-Zag Star Pattern (3-row zigzag)
Input: n = 15 (number of columns)
Output:
* * *
******
* * * *

public class ZigZag {


public static void main(String[] args) {
int n = 15; // number of positions
int rows = 3;
char[][] a = new char[rows][n];
// fill with spaces
for (int i=0;i<rows;i++) for (int j=0;j<n;j++) a[i][j] = ' ';
int row = 0; int dir = 1;
for (int col = 0; col < n; col++) {
a[row][col] = '*';
if (row == 0) dir = 1;
else if (row == rows-1) dir = -1;
row += dir;
}
// print
for (int i = 0; i < rows; i++) {
for (int j = 0; j < n; j++) System.out.print(a[i][j]);
System.out.println();
}
}
}
4 — Hollow Diamond (even/odd safe)
Input: n = 5 (height/top half)
Output:
*
**
* *
* *
* *
* *
* *
**
*

public class HollowDiamond {


public static void main(String[] args) {
int n = 5; // rows in top half
// upper including middle
for (int i = 1; i <= n; i++) {
for (int s = i; s < n; s++) System.out.print(" ");
if (i == 1) System.out.println("*");
else {
System.out.print("*");
for (int sp = 1; sp <= 2*i - 3; sp++) System.out.print(" ");
System.out.println("*");
}
}
// lower
for (int i = n - 1; i >= 1; i--) {
for (int s = i; s < n; s++) System.out.print(" ");
if (i == 1) System.out.println("*");
else {
System.out.print("*");
for (int sp = 1; sp <= 2*i - 3; sp++) System.out.print(" ");
System.out.println("*");
}
}
}
}

5 — Hourglass Number Pattern


Input: n = 5
Output:
123454321
1234321
12321
121
1
121
12321
1234321
123454321

Solution (Java):
public class HourglassNumber {
public static void main(String[] args) {
int n = 5;
// upper including middle
for (int i = 0; i < n; i++) {
for (int s = 0; s < i; s++) System.out.print(" ");
for (int k = 1; k <= n - i; k++) System.out.print(k);
for (int k = n - i - 1; k >= 1; k--) System.out.print(k);
System.out.println();
}
// lower
for (int i = n - 2; i >= 0; i--) {
for (int s = 0; s < i; s++) System.out.print(" ");
for (int k = 1; k <= n - i; k++) System.out.print(k);
for (int k = n - i - 1; k >= 1; k--) System.out.print(k);
System.out.println();
}
}
}

6 — Hollow Square with Diagonals (general n)


Input: n = 7
Output:
*******
** **
****
* * *
****
** **
*******
Solution (Java):
public class HollowSquareDiagonals {
public static void main(String[] args) {
int n = 7;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0 || i == n-1 || j == n-1 || i==j || i+j==n-1) System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
}
}

7 — Mirror Number Pyramid with Spaces


Input: n = 6
Output:
1
212
32123
4321234
543212345
65432123456

public class MirrorNumberPyramid {


public static void main(String[] args) {
int n = 6;
for (int i = 1; i <= n; i++) {
for (int sp = i; sp < n; sp++) System.out.print(" "); // two spaces for alignment
for (int k = i; k >= 1; k--) System.out.print(k + " ");
for (int k = 2; k <= i; k++) System.out.print(k + (k==i ? "" : " "));
System.out.println();
}
}
}
8 — Numeric Diamond (incremental counter)
Input: n = 4
Output:
1
23
456
7 8 9 10
456
23
1

public class NumericDiamond {


public static void main(String[] args) {
int n = 4;
int counter = 1;
// top
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) System.out.print((counter++) + (j==i? "" : " "));
System.out.println();
}
// bottom
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) System.out.print((counter - (i*(i+1)/2)) + (j==i? "" : " "));
// adjust counter to next sequence start
counter = counter - (i*(i+1)/2);
// advance counter to reflect printed numbers
for (int j = 1; j <= i; j++) counter++;
System.out.println();
}
}
}
9 — Floyd’s Triangle but Right-Aligned (wide numbers)
Input: n = 6
Output:
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

public class FloydRightAlign {


public static void main(String[] args) {
int n = 6;
int count = 1;
// compute width for alignment (length of last number)
int maxNum = n*(n+1)/2;
int width = String.valueOf(maxNum).length() + 1; // +1 for a trailing space
for (int i = 1; i <= n; i++) {
// left padding
int pad = (n - i) * width;
for (int p = 0; p < pad; p++) System.out.print(" ");
for (int j = 1; j <= i; j++) {
String s = String.valueOf(count++);
// pad each number to width (right aligned)
System.out.print(" ".repeat(width - s.length()) + s);
}
System.out.println();
}
}
}
10 — Star Hourglass (solid)
Input: n = 5
Output:
*********
*******
*****
***
*
***
*****
*******
*********

public class StarHourglass {


public static void main(String[] args) {
int n = 5;
// top including middle
for (int i = 0; i < n; i++) {
for (int s = 0; s < i; s++) System.out.print(" ");
for (int k = 0; k < 2*(n - i) - 1; k++) System.out.print("*");
System.out.println();
}
// bottom
for (int i = 2; i <= n; i++) {
for (int s = 0; s < n - i; s++) System.out.print(" ");
for (int k = 0; k < 2*i - 1; k++) System.out.print("*");
System.out.println();
}
}
}
11 — Square Number Spiral (clockwise) — small size demo
Input: n = 4
Output:
1234
12 13 14 5
11 16 15 6
10 9 8 7

public class NumberSpiral {


public static void main(String[] args) {
int n = 4;
int[][] a = new int[n][n];
int top = 0, bottom = n-1, left = 0, right = n-1;
int num = 1;
while (true) {
if (left > right) break;
for (int j = left; j <= right; j++) a[top][j] = num++;
top++;
if (top > bottom) break;
for (int i = top; i <= bottom; i++) a[i][right] = num++;
right--;
if (left > right) break;
for (int j = right; j >= left; j--) a[bottom][j] = num++;
bottom--;
if (top > bottom) break;
for (int i = bottom; i >= top; i--) a[i][left] = num++;
left++;
}
for (int i=0;i<n;i++){
for (int j=0;j<n;j++) System.out.print(a[i][j] + (j==n-1? "" : " "));
System.out.println();
}
}
}

12 — Triangle of Prime Flags (mark primes with *)


Input: n = 7 (print first n numbers in triangle form)
Output (n=7 triangle):
2
35
7 11 13
17 19 23 29
31 37 41 43 47
53 59 61 67 71 73
79 83 89 97 101 103 107

public class PrimeTriangle {


public static boolean isPrime(int x) {
if (x < 2) return false;
for (int i = 2; i*i <= x; i++) if (x % i == 0) return false;
return true;
}
public static void main(String[] args) {
int rows = 7;
int count = 0;
int num = 2;
for (int r = 1; r <= rows; r++) {
int printed = 0;
while (printed < r) {
if (isPrime(num)) {
System.out.print(num + (printed == r-1 ? "" : " "));
printed++;
}
num++;
}
System.out.println();
}
}
}

You might also like