0% found this document useful (0 votes)
20 views33 pages

Star and Number Patterns in Java/Python

The document provides a collection of 30 pattern programs in Java and Python, showcasing various shapes formed using stars and numbers. Examples include right-angled triangles, pyramids, diamonds, and more complex patterns like hollow shapes and Floyd's triangle. Each pattern is accompanied by code snippets for both Java and Python, illustrating how to generate the respective patterns.

Uploaded by

sapigif483
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)
20 views33 pages

Star and Number Patterns in Java/Python

The document provides a collection of 30 pattern programs in Java and Python, showcasing various shapes formed using stars and numbers. Examples include right-angled triangles, pyramids, diamonds, and more complex patterns like hollow shapes and Floyd's triangle. Each pattern is accompanied by code snippets for both Java and Python, illustrating how to generate the respective patterns.

Uploaded by

sapigif483
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

Code Yatra

30 Pattern Programs

1. Right-Angled Triangle

This pattern forms a right-angled triangle with stars (*).

**
***

****

*****

JAVA

public class RightAngleTriangle {

public static void main(String[] args) {

int n = 5; // Number of rows

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

[Link]("* ");

}
[Link]();

}
}

Python n = 5 # Number

of rows for i in range(1,

n + 1): for j in

range(i): print("*",

end=" ")
print()
Inverted Right-Angled Triangle

This pattern forms an inverted right-angled triangle with stars (*).

***** ****

*** **

Java public class InvertedTriangle {

public static void main(String[] args) {

int n = 5; // Number of rows

for (int i = n; i >= 1; i--) {

for (int j = 1; j <= i; j++) {

[Link]("* ");
}

[Link]();
}

Python n = 5 # Number

of rows for i in range(n,

0, -1): for j in range(i):

print("*", end=" ")

print()

Pyramid Pattern

This pattern forms a pyramid with stars ( *).

*
3.
***

*****

*******

*********

Java
public class PyramidPattern { public
static void main(String[] args) { int
n = 5; // Number of rows for (int i =
1; i <= n; i++) {
for (int j = i; j < n; j++) {
[Link](" "); // Print spaces
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*"); // Print stars
}
[Link]();
}
}
}
Python

n = 5 # Number of rows for i in


range(1, n + 1): print(" " * (n - i) +

"*" * (2 * i - 1))

Diamond Pattern

This pattern forms a diamond shape with stars ( *).

***
*****

*******

*********

*******

*****

***

Java
public class DiamondPattern { public
static void main(String[] args) { int
n = 5; // Half number of rows //
Upper part of the diamond for (int
i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
[Link](" "); // Print spaces
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*"); // Print stars
}
[Link]();
}
// Lower part of the diamond
for (int i = n - 1; i >= 1; i--) {
for (int j = n; j > i; j--) {
[Link](" "); // Print spaces
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*"); // Print stars
}
[Link]();
}
}
}
Python
n = 5 # Half number of rows #
Upper part of the diamond for i in
range(1, n + 1): print(" " * (n - i) +
"*" * (2 * i - 1))
# Lower part of the diamond for
i in range(n - 1, 0, -1):
print(" " * (n - i) + "*" * (2 * i - 1))
5. Number Pyramid
This pattern prints numbers instead of stars, forming a pyramid.

222

33333

4444444

555555555

Java

public class NumberPyramid { public


static void main(String[] args) { int
n = 5; // Number of rows for (int i =
1; i <= n; i++) { for (int j = i; j <
n; j++) {
[Link](" "); // Print spaces
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link](i); // Print numbers
}
[Link]();
}
}
}

Python

n = 5 # Number of rows for i in

range(1, n + 1): print(" " * (n - i) +

str(i) * (2 * i - 1))
6. Right-Angled Triangle (with numbers)
This pattern uses numbers rather than stars.

12

123

1234

12345

Java

public class NumberTriangle { public


static void main(String[] args) { int
n = 5; // Number of rows for (int i =
1; i <= n; i++) { for (int j = 1; j
<= i; j++) { [Link](j
+ " ");
}
[Link]();
}
}
}

Python

n = 5 # Number of rows

for i in range(1, n + 1):

for j in range(1, i + 1):

print(j, end=" ")

print()
7 Inverted Number Triangle
This pattern starts with a larger set of numbers and decreases with each row.

12345

1234

123

12

Java
public class InvertedNumberTriangle {
public static void main(String[] args) {
int n = 5; // Number of rows for (int
i = n; i >= 1; i--) { for (int j = 1; j
<= i; j++) { [Link](j
+ " ");
}
[Link]();
}
}
}

Python

n = 5 # Number of rows

for i in range(n, 0, -1):

for j in range(1, i + 1):

print(j, end=" ") print()


8 Hollow Diamond
A more complex pattern where stars form a hollow diamond shape.

* *

* *

* *

*********

* *

* *

* *

Java

public class HollowDiamond { public


static void main(String[] args) { int
n = 5; // Half number of rows //
Upper part of the hollow diamond
for (int i = 1; i <= n; i++) { for
(int j = i; j < n; j++) {
[Link](" "); // Print spaces
}
for (int k = 1; k <= (2 * i - 1); k++) {
if (k == 1 || k == (2 * i - 1)) {
[Link]("*"); // Print stars
} else {
[Link](" "); // Hollow space
}
}
[Link]();
}
// Lower part of the hollow diamond
for (int i = n - 1; i >= 1; i--) { for
(int j = n; j > i; j--) {
[Link](" "); // Print spaces
}
for (int k = 1; k <= (2 * i - 1); k++) {
if (k == 1 || k == (2 * i - 1)) {
[Link]("*"); // Print stars
} else {
[Link](" "); // Hollow space
}
}
[Link]();
}
}}

Python

n = 5 # Half number of rows #


Upper part of the hollow diamond
for i in range(1, n + 1): print(" " *
(n - i), end="") for j in range(1, 2 *
i): if j == 1 or j == (2 * i - 1):
print("*", end="") else:
print(" ", end="")
print()
# Lower part of the hollow diamond
for i in range(n - 1, 0, -1): print(" "
* (n - i), end="") for j in range(1, 2
* i): if j == 1 or j == (2 * i - 1):
print("*", end="") else:
print(" ", end="")
print()
9 Floyd's Triangle (with Numbers)
This pattern is a number-based triangle where each row contains sequential numbers.

23

456

7 8 9 10

11 12 13 14 15 Java

public class FloydsTriangle { public


static void main(String[] args) { int
n = 5; // Number of rows int num =
1; // Start with number 1 for (int i =
1; i <= n; i++) { for (int j = 1; j
<= i; j++) {
[Link](num + " ");
num++;
}
[Link]();
}
}
}

Python

n = 5 # Number of rows num


= 1 # Start with number 1 for i
in range(1, n + 1): for j in
range(1, i + 1): print(num,
end=" ") num += 1
print()
10 Butterfly Pattern
A butterfly-shaped pattern made with stars.

* *

** **

*** ***

**** ****

*********

**** ****

*** ***

** **

* *

Java

public class ButterflyPattern { public


static void main(String[] args) {
int n = 5; // Number of rows //
Upper part of the butterfly for
(int i = 1; i <= n; i++) { for (int
j = 1; j <= i; j++) {
[Link]("*");
}
for (int j = 1; j <= 2 * (n - i); j++) {
[Link](" ");
}
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
// Lower part of the butterfly
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
for (int j = 1; j <= 2 * (n - i); j++) {
[Link](" ");
}
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}

Python

n = 5 # Number of rows # Upper part of


the butterfly for i in range(1, n + 1):
print("*" * i + " " * (2 * (n - i)) + "*" * i) #
Lower part of the butterfly for i in range(n
- 1, 0, -1): print("*" * i + " " * (2 * (n -
i)) + "*" * i)
11 Two Diagonals Crossing Each Other (Stars)

* * * *
*
* * * *

Java

public class DiagonalCrossPattern {


public static void main(String[] args) {
int n = 5; // Size of the square matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Conditions for the two diagonals
if (i == j || i + j == n - 1) {
[Link]("* "); // Star at diagonal
} else {
[Link](" "); // Space elsewhere
}
}
[Link]();
}
}
}

Python

n = 5 # Size of the square matrix


for i in range(n): for j in
range(n):
# Conditions for the two diagonals
if i == j or i + j == n - 1:
print("*", end=" ") # Star at diagonal
else:
print(" ", end=" ") # Space elsewhere
print()
12 Square Pattern with 5 Lines and 5 Stars in Each Line
*****
*****
*****
*****
*****

public class SquarePattern { public static void


main(String[] args) { int n = 5; // Number of rows
and columns (5x5 grid) for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
[Link]("* "); // Print star in each column
}
[Link](); // Move to the next line
}
}
}

n = 5 # Number of rows and columns (5x5 grid)


for i in range(n): for j in range(n):
print("*", end=" ") # Print star in each column
print() # Move to the next line
13 Square Pattern with Border and Empty Center

* ****
* *
* *
* *
* ****

Java
public class HollowSquarePattern {
public static void main(String[] args) {
int n = 5; // Size of the square (5x5 grid)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Print stars on the border, otherwise print space
if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
[Link]("* ");
} else {
[Link](" ");
}
}
[Link](); // Move to the next line
}
}
}

Python

n=5 # Size of the square (5x5 grid)


for i in range(n): for j in range(n):
# Print stars on the border, otherwise print space
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print() # Move to the next line

14. You are given a pattern that alternates between rows of "X"s and rows of numbers.
The numbers start at 9 and decrease by 2 with each subsequent occurrence. Your task is to
print the given pattern using a programming language of your choice. The pattern looks
like this:

XXXXXXXXXX
9999999999
XXXXXXXXXX
7777777777
XXXXXXXXXX
6666666666

Java

public class PatternPrinter { public


static void main(String[] args) {
int[] numbers = {9, 7, 6}; // The numbers to print
int numLines = [Link]; // How many lines with numbers
int length = 10; // Length of the "X" string and each number string

// Loop through the pattern


for (int i = 0; i < numLines; i++) {
if (i % 2 == 0) {
// Print "X" for even index rows (0, 2, 4, ...)
for (int j = 0; j < length; j++) {
[Link]("X");
}
} else {
// Print number for odd index rows (1, 3, 5, ...)
for (int j = 0; j < length; j++) {
[Link](numbers[i / 2]);
}
}
[Link](); // Move to the next line after each row
}
}
}

Python
def print_pattern():
numbers = [9, 7, 6] # The numbers to print length = 10
# Length of each "X" string and number string

for i in range(len(numbers) + 1): # Looping through the pattern


if i % 2 == 0:
# Print "X" for even index rows (0, 2, 4, ...)
print("X" * length) else:
# Print the number for odd index rows (1, 3, 5, ...)
print(str(numbers[i // 2]) * length)

# Call the function to print the pattern print_pattern()


15 .Your program must be dynamic It should work for any odd numbers (excluding 1 and any
negative numbers). It should not run for any even numbers. for the second line it should be 3 gap..
3rd line 1

*******
** ** n=7
*** ***
*******
*** ***
** **
*******

*********
** **
*** *** n= 9
*********
*** ***
** **
*********

Java

import [Link];

public class DynamicPatternPrinter {

public static void main(String[] args) { //


Input Scanner for the number of columns
Scanner scanner = new Scanner([Link]);
[Link]("Enter an odd number greater than 1: ");
int n = [Link]();

// Check if the number is valid (odd and greater than 1)


if (n <= 1 || n % 2 == 0) {
[Link]("Invalid input! The number should be an odd number greater than 1.");
return;
}

// Loop through the rows


for (int i = 0; i < n; i++) {
if (i == 0 || i == n - 1) {
// Print the first and last row with all stars
for (int j = 0; j < n; j++) {
[Link]("*");
}
}
else if (i == 1 || i == n - 2) {
// Print the second and second-to-last row: **...** with (n-4) spaces
[Link]("**"); for (int j = 0; j < n - 4; j++) {
[Link](" ");
}
[Link]("**");
}
else if (i == 2 || i == n - 3) {
// Print the third and fifth row: ***...*** with (n-6) spaces
[Link]("***"); for (int j = 0; j < n - 6; j++) {
[Link](" ");
}
[Link]("***");
}
else if (i == n / 2) {
// Print the middle row: all stars
for (int j = 0; j < n; j++) {
[Link]("*");
}
}

// Move to the next line after each row


[Link]();
}

[Link](); // Close the scanner


}
}

Python

def print_pattern():
# Input for the number of columns
n = int(input("Enter an odd number greater than 1: "))

# Check if the number is valid (odd and greater than 1)


if n <= 1 or n % 2 == 0:
print("Invalid input! The number should be an odd number greater than 1.")
return
# Loop through the rows
for i in range(n):
if i == 0 or i == n - 1:
# Print the first and last row with all stars
print("*" * n)
elif i == 1 or i == n - 2:
# Print the second and second-to-last row: **...** with (n-4) spaces
print("**" + " " * (n - 4) + "**") elif i == 2 or i == n - 3:
# Print the third and fifth row: ***...*** with (n-6) spaces
print("***" + " " * (n - 6) + "***")
elif i == n // 2:
# Print the middle row: all stars
print("*" * n)

# Call the function to print the pattern print_pattern()


16.

Number Pyramid

1
121
12321
1234321
123454321

Java

public class NumberPyramid { public


static void main(String[] args) { int
n = 5; // Number of rows for (int i
= 1; i <= n; i++) { for (int j = 1; j
<= i; j++) {
[Link](j);
}
for (int j = i - 1; j >= 1; j--) {
[Link](j);
}
[Link]();
}
}
}

Python

def number_pyramid(n):
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end="") for j in
range(i - 1, 0, -1):
print(j, end="")
print()

number_pyramid(5)

Hollow Inverted Right Angle Triangle

*****
* *
17.

* *
* *
*

Java

public class HollowInvertedRightAngle {


public static void main(String[] args) {
int n = 5; for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) { if (j
== 1 || j == i || i == n) {
[Link]("*");
} else {
[Link](" ");
}
}
[Link]();
}
}
}

Python

def hollow_inverted_right_angle(n):
for i in range(n, 0, -1): for j in
range(1, i + 1): if j == 1 or j ==
i or i == n: print("*",
end="") else:
print(" ", end="")
print()

hollow_inverted_right_angle(5)
Number Spiral

1234
5678
9 10 11 12
13 14 15 16

Java
18.

public class NumberSpiral {


public static void main(String[] args) {
int n = 4; int[][] matrix = new
int[n][n]; int num = 1;

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
matrix[i][j] = num++;
}
}

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}

Python

def number_spiral(n):
matrix = [[0] * n for _ in range(n)]
num = 1 for i in range(n): for
j in range(n): matrix[i][j] =
num
num += 1

for row in matrix:


print(" ".join(map(str, row)))

number_spiral(4)
Pascal’s Triangle

1
11
121
1331
14641
19.

Java

public class PascalsTriangle {


public static void main(String[] args) {
int n = 5;
int[][] triangle = new int[n][n];

for (int i = 0; i < n; i++) {


triangle[i][0] = triangle[i][i] = 1;
for (int j = 1; j < i; j++) {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}

for (int i = 0; i < n; i++) {


for (int j = 0; j <= i; j++) {
[Link](triangle[i][j] + " ");
}
[Link]();
}
}
}

Python

def pascals_triangle(n):
triangle = [[0] * n for _ in range(n)]
for i in range(n):
triangle[i][0] = triangle[i][i] = 1
for j in range(1, i):
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]

for i in range(n):
print(" ".join(map(str, triangle[i][:i+1])))

pascals_triangle(5)
Hourglass with Stars

*******
*****
***
*
20.

***
*****
*******

Java

public class HourglassWithStars {


public static void main(String[] args) {
int n = 7; for (int i = 0; i < n / 2; i++)
{ for (int j = 0; j < i; j++) {
[Link](" ");
}
for (int j = 0; j < n - 2 * i; j++) {
[Link]("*");
}
[Link]();
}
for (int i = n / 2; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
[Link](" ");
}
for (int j = 0; j < 2 * i - n + 1; j++) {
[Link]("*");
}
[Link]();
}
}
}

Python

def hourglass_with_stars(n):
for i in range(n // 2):
print(" " * i + "*" * (n - 2 * i))
for i in range(n // 2, n):
print(" " * (n - i - 1) + "*" * (2 * i - n + 1))

hourglass_with_stars(7)
Zigzag Number Pattern

12345
6789
21.

10 11 12
13 14
15

Java

public class ZigzagNumberPattern {


public static void main(String[] args) {
int n = 5; int num = 1; for (int i
= 0; i < n; i++) { for (int j = 0; j < i;
j++) { [Link](" ");
}
for (int j = i; j < n; j++) {
[Link](num++ + " ");
}
[Link]();
}
}
}

Python

def zigzag_number_pattern(n):
num = 1 for i in range(n): print(" " * i + "
".join(str(num + j) for j in range(n - i))) num += (n - i)

zigzag_number_pattern(5)

Hollow Number Square

1 2 3 4
5 6
7 8
22.

9 10 11 12

Java

public class HollowNumberSquare {


public static void main(String[] args) {
int n = 4; // Size of the square int
num = 1; for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
[Link](num++ + " ");
} else {
[Link](" ");
}
}
[Link]();
}
}
}

Python

def hollow_number_square(n):
num = 1 for i in range(n): for j in
range(n): if i == 0 or i == n - 1 or j == 0 or
j == n - 1:
print(num, end=" ")
num += 1 else:
print(" ", end=" ")
print()

hollow_number_square(4)
23 Hollow Diamond

1
121
1 1
121 121
1 1
121
1

Java

public class HollowDiamond { public static


void main(String[] args) { int n = 5; //
Middle row of the diamond // Upper
half of the diamond for (int i = 1; i <= n;
i++) { for (int j = i; j < n; j++) {
[Link](" ");
}
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i) {
[Link](j);
} else {
[Link](" ");
}
}
for (int j = i - 1; j >= 1; j--) {
if (j == 1 || j == i) {
[Link](j);
} else {
[Link](" ");
}
}
[Link]();
}
// Lower half of the diamond
for (int i = n - 1; i >= 1; i--) {
for (int j = i; j < n; j++) {
[Link](" ");
}
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i) {
[Link](j); }
else {
[Link](" ");
}
} for (int j = i - 1; j
>= 1; j--) { if (j == 1 || j
== i) {
[Link](j);
} else {
[Link](" ");
}
}
[Link]();
}
}
}

Python

def hollow_diamond(n): #
Upper half of the diamond
for i in range(1, n + 1):
print(" " * (n - i), end="")
for j in range(1, i + 1): if
j == 1 or j == i: print(j,
end="") else:
print(" ", end="")
for j in range(i - 1, 0, -1):
if j == 1 or j == i:
print(j, end="") else:
print(" ", end="")
print()

# Lower half of the diamond


for i in range(n - 1, 0, -1):
print(" " * (n - i), end="")
for j in range(1, i + 1): if
j == 1 or j == i: print(j,
end="") else:
print(" ", end="")
for j in range(i - 1, 0, -1):
if j == 1 or j == i:
print(j, end="") else:
print(" ", end="")
print()
hollow_diamond(5) 24 Spiral of Numbers
(Counterclockwise)

123
894
765

Java

public class NumberSpiralCounterclockwise {


public static void main(String[] args) { int n =
3; // Size of the matrix (should be odd)
int[][] spiral = new int[n][n];
int num = 1;
int top = 0, bottom = n - 1, left = 0, right = n - 1;

while (top <= bottom && left <= right) {


// Fill the left column for
(int i = top; i <= bottom; i++) {
spiral[i][left] = num++;
}
left++;

// Fill the bottom row


for (int i = left; i <= right; i++) {
spiral[bottom][i] = num++;
}
bottom--;

// Fill the right column


for (int i = bottom; i >= top; i--) {
spiral[i][right] = num++;
}
right--;

// Fill the top row for


(int i = right; i >= left; i--) {
spiral[top][i] = num++;
}
top++;
}

// Print the spiral matrix


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
[Link](spiral[i][j] + " ");
}
[Link]();
}
}
}

Python

def number_spiral_counterclockwise(n):
spiral = [[0] * n for _ in range(n)] num
=1
top, bottom, left, right = 0, n - 1, 0, n - 1

while top <= bottom and left <= right:


# Fill the left column for i in
range(top, bottom + 1):
spiral[i][left] = num
num += 1
left += 1

# Fill the bottom row


for i in range(left, right + 1):
spiral[bottom][i] = num
num += 1 bottom -= 1

# Fill the right column for i in


range(bottom, top - 1, -1):
spiral[i][right] = num
num += 1
right -= 1

# Fill the top row for i in


range(right, left - 1, -1):
spiral[top][i] = num
num += 1
top += 1

# Print the spiral


for row in spiral:
print(" ".join(map(str, row)))

number_spiral_counterclockwise(3)
25 Alternating Odd and Even Numbers in a Square

Java

1212
2121
1212
2121

public class AlternatingOddEvenSquare {


public static void main(String[] args) {
int n = 4; // Size of the square for
(int i = 0; i < n; i++) { for (int j = 0; j
< n; j++) { if ((i + j) % 2 == 0) {
[Link](1 + " ");
} else {
[Link](2 + " ");
}
}
[Link]();
}
}
}

Python

def alternating_odd_even_square(n):
for i in range(n): for j in range(n):
if (i + j) % 2 == 0: print(1,
end=" ") else:
print(2, end=" ")
print()

alternating_odd_even_square(4)

Common questions

Powered by AI

The computational complexity for generating both pyramid and diamond patterns is similar in that both involve nested loops to handle rows and spaces. However, the diamond pattern is slightly more complex due to requiring additional iterations for both the upper and lower sections, whereas the pyramid pattern only has a singular ascending section. Both patterns are O(n^2) in terms of operations, but the diamond requires roughly twice the iterations due to its symmetric nature .

Implementing a counterclockwise number spiral presents challenges in maintaining accurate index bounds while performing coordinate translation for correct spiral progression. Efficiently managing boundary updates and direction changes through conditional structures requires meticulous debugging, as logic errors can result in off-bound or skipped array fills, complicating asynchronous indexing and looping constructs .

Symmetry in both the diamond and butterfly patterns is achieved by mirroring the top part below a horizontal axis. In the diamond, this creates a seamless shape with equal number of stars above and below the middle row, forming a perfect geometric diamond shape. The butterfly pattern uses symmetry to form a visual representation that resembles butterfly wings, where the outer parts diverge and then converge, simulating the wing shape .

Pascal's Triangle is pivotal in generating binomial coefficients, which underpin many recursive structures seen in combinatorial problems and pattern generation. The triangle's symmetrical properties and orderly growth through addition offer a blueprint for constructing patterns with numerical symmetry. Applications include designing pyramids with coefficients representing row arrangements, enhancing recognition of mathematical series in patterns .

Nested loops provide clear visibility into each step of pattern building by explicitly controlling iterations and conditions. However, they can be less efficient and more verbose. String multiplication in Python, which concatenates repeated characters or spaces concisely, enhances readability and can reduce execution time by minimizing loop overhead. Yet, it trades off some explicit control over individual element positioning .

The Zigzag Number Pattern creates visual complexity by arranging numbers diagonally rather than in straight lines, challenging the standard left-to-right reading habit. This can affect comprehension, as users need to adjust to follow numbers along unconventional paths, increasing cognitive load for pattern recognition compared to linear arrangements .

An alternative approach to constructing a number pyramid is to use string manipulation functions to calculate and print spaces and characters directly rather than using nested loops for printing spaces and numbers separately. This can make the code more concise and potentially improve readability by reducing nesting, but it might complicate the understanding of how the pattern is formed, as the logic is less explicit .

The hollow diamond pattern differs from the solid diamond pattern by forming a diamond shape with stars only at the edges, leaving the interior hollow, whereas the solid diamond is completely filled with stars. This is achieved in the hollow diamond by printing stars only at positions where k equals 1 or (2*i -1) during the star printing loops, and using spaces for other positions .

Changing the base case from n=5 to another number will directly impact the number of rows in the hollow diamond, affecting its overall height and width. An increase in n will result in a taller and wider diamond with more rows to form the hollow interior, thereby increasing visual complexity. Each side of the diamond will extend proportionally, requiring more spaces between the outline stars .

Both the right-angled triangle and inverted right-angled triangle patterns share the characteristic of having equal numbers of elements on each side of the longest horizontal or vertical line, creating a right angle. However, the right-angled triangle starts with one element and increases in each subsequent row, while the inverted triangle decreases, starting with the maximum number .

You might also like