100% found this document useful (1 vote)
3K views124 pages

100 Pattern Programs in PHP Shahrukh Khan

This document contains 100 pattern programs in PHP with examples ranging from basic warmup exercises to more complex geometric patterns like squares, triangles, letters of the alphabet and numbers. The patterns are created using stars and include options for solid, hollow, mirrored and inverted shapes. The programs are broken into sections based on type of pattern like warmup, geometric, numbers and alphabet patterns. The document serves as a reference for learning pattern programming in PHP.

Uploaded by

Inke
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
100% found this document useful (1 vote)
3K views124 pages

100 Pattern Programs in PHP Shahrukh Khan

This document contains 100 pattern programs in PHP with examples ranging from basic warmup exercises to more complex geometric patterns like squares, triangles, letters of the alphabet and numbers. The patterns are created using stars and include options for solid, hollow, mirrored and inverted shapes. The programs are broken into sections based on type of pattern like warmup, geometric, numbers and alphabet patterns. The document serves as a reference for learning pattern programming in PHP.

Uploaded by

Inke
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/ 124

100

PATTERN PROGRAMS IN PHP

SHAHRUKH KHAN
www.thesoftwareguy.in
100 PATTERN PROGRAMS IN PHP

Table of Contents
About the author ........................................................................................................................................... 6
Preface ............................................................................................................................................................... 8
Resources ......................................................................................................................................................... 9
A. Warmup Exercises ............................................................................................................................ 10
1. Program to print your name five times ................................................................................ 10
2. Program to print numbers from 1 to 10 but skipping numbers 4 and 6 ................. 11
3. Program to print 4 X 3 grid with hello world ..................................................................... 12
4. Program to print 3 X 5 table dynamically in PHP............................................................. 13
5. Program to print table from 1 to 10 ...................................................................................... 14
B. Geometrical Pattern......................................................................................................................... 15
1. Program to print square with star pattern ......................................................................... 16
2. Program to print square with number pattern ................................................................. 17
3. Program to print square with number pattern ................................................................. 18
4. Program to print square with alphabet pattern ............................................................... 19
5. Program to print square with alphabet pattern ............................................................... 20
6. Program to print hollow square with star pattern .......................................................... 21
7. Program to print rectangle shape with star pattern ....................................................... 22
8. Program to print hollow rectangle with star pattern ..................................................... 23
9. Program to print right-angle triangle with star pattern................................................ 24
10. Program to print right-angle triangle with number pattern ................................... 25
11. Program to print inverted right-angle triangle with star pattern ......................... 26
12. Program to print inverted right-angle triangle with number pattern................. 27
13. Program to print mirror right-angle triangle with star pattern ............................. 28
14. Program to print mirror right-angle triangle with number pattern ..................... 29
15. Program to print inverted mirror right-angle triangle with star pattern .......... 30
16. Program to print inverted hollow mirror right-angle triangle with star pattern
31
17. Program to print hollow right-angle triangle with star pattern ............................ 32
18. Program to print hollow inverted right-angle triangle with star pattern .......... 33
19. Program to print hollow mirror right angle triangle with star pattern .............. 34
20. Program to print rhombus with star pattern................................................................. 35
21. Program to print hollow rhombus with star .................................................................. 36
22. Program to print pyramid with star pattern .................................................................. 37

SHAHRUKH KHAN www.thesoftwareguy.in 2


100 PATTERN PROGRAMS IN PHP

23. Program to print inverted pyramid with star pattern ............................................... 38


24. Program to print hollow pyramid with star pattern ................................................... 39
25. Program to print inverted hollow pyramid with star pattern ................................ 40
26. Program to print acute triangle with star pattern – style 1 .................................... 41
27. Program to print acute triangle with star pattern – style 2 .................................... 42
28. Program to print hollow diamond with star................................................................... 43
29. Program to print hourglass with star pattern ............................................................... 45
30. Program to print hollow diamond with star................................................................... 46
C. Numbers Pattern ............................................................................................................................... 48
1. Program to print number one with star pattern .............................................................. 49
2. Program to print number two with star pattern .............................................................. 50
3. Program to print number three with star pattern ........................................................... 51
4. Program to print number four with star pattern ............................................................. 52
5. Program to print number five with star pattern .............................................................. 53
6. Program to print number six with star pattern ................................................................. 54
7. Program to print number seven with star pattern .......................................................... 56
8. Program to print number eight with star pattern ............................................................ 57
9. Program to print number nine with star pattern ............................................................. 58
10. Program to print number zero with star pattern ......................................................... 59
D. Alphabet Pattern ........................................................................................................................... 60
1. Program to print letter A with star pattern ........................................................................ 61
2. Program to print letter B with star pattern ........................................................................ 62
3. Program to print letter C with star pattern ........................................................................ 63
4. Program to print letter D with star pattern ....................................................................... 64
5. Program to print letter E with star pattern ........................................................................ 65
6. Program to print letter F with star pattern ........................................................................ 66
7. Program to print letter G with star pattern ....................................................................... 67
8. Program to print letter H with star pattern ....................................................................... 68
9. Program to print letter I with star pattern ......................................................................... 70
10. Program to print letter J with star pattern .................................................................... 71
11. Program to print letter K with star pattern ................................................................... 72
12. Program to print letter L with star pattern .................................................................... 74
13. Program to print letter M with star pattern .................................................................. 75
14. Program to print letter N with star pattern ................................................................... 76
15. Program to print letter O with star pattern ................................................................... 77

SHAHRUKH KHAN www.thesoftwareguy.in 3


100 PATTERN PROGRAMS IN PHP

16. Program to print letter P with star pattern.................................................................... 78


17. Program to print letter Q with star pattern ................................................................... 79
18. Program to print letter R with star pattern ................................................................... 80
19. Program to print letter S with star pattern .................................................................... 82
20. Program to print letter T with star pattern .................................................................... 83
21. Program to print letter U with star pattern ................................................................... 84
22. Program to print letter V with star pattern ................................................................... 85
23. Program to print letter W with star pattern .................................................................. 87
24. Program to print letter X with star pattern ................................................................... 88
25. Program to print letter Y with star pattern ................................................................... 90
26. Program to print letter Z with star pattern ................................................................... 92
E. Mixed Sets ............................................................................................................................................ 93
1. Program to print HTML table maintaining proper tags under given conditions 94
2. Program to print Floyd triangle............................................................................................... 96
3. Program to print pascal triangle ............................................................................................. 97
4. Program to print the pattern given below .......................................................................... 98
5. Program to print the pattern given below .......................................................................... 99
6. Program to print the pattern given below ........................................................................ 100
7. Program to print the pattern given below ........................................................................ 101
8. Program to print the pattern given below ........................................................................ 102
9. Program to print the pattern given below ........................................................................ 103
10. Program to print the pattern given below .................................................................... 104
11. Program to print the pattern given below .................................................................... 105
12. Program to print the pattern given below .................................................................... 106
13. Program to print the full diamond with star pattern ............................................... 107
14. Program to print the alphabet pattern given below................................................. 108
15. Program to print the alphabet pattern given below................................................. 109
16. Program to print the alphabet pattern given below................................................. 110
17. Program to print the alphabet pattern given below................................................. 111
18. Program to print the alphabet pattern given below................................................. 112
19. Program to print the half diamond with alphabet pattern .................................... 113
20. Program to print the pyramid with alphabet pattern .............................................. 114
21. Program to print the pattern given below .................................................................... 115
22. Program to print the pattern given below .................................................................... 116
23. Program to print the alphanumeric pattern given below ...................................... 117

SHAHRUKH KHAN www.thesoftwareguy.in 4


100 PATTERN PROGRAMS IN PHP

24. Program to print the pattern given below .................................................................... 118


25. Program to print the pattern given below .................................................................... 119
26. Program to print the pattern given below .................................................................... 120
27. Program to print the pattern given below .................................................................... 121
28. Program to print the number pattern given below................................................... 122
29. Program to print the number pattern given below................................................... 123
Feedback ...................................................................................................................................................... 124

SHAHRUKH KHAN www.thesoftwareguy.in 5


100 PATTERN PROGRAMS IN PHP

About the author


Hello Readers,

My name is Shahrukh Khan. I am an Entrepreneur, PHP Web Developer, and Blogger


from Kolkata, India. I have a master’s degree in Computer Application (MCA).

Building Software, Websites & Web Apps


is my passion. I have around 10 years of
experience in the IT field working mostly
on PHP and related environment.

I have worked for clients around the


globe providing them customized web
solutions. I write blogs at
https://www.thesoftwareguy.in , over the
years, I have enjoyed a lot interacting
with the community, handling queries and
solving their problems which indirectly
helped me get a great experience.

Although I have written a lot of articles on my blog, but this is my first attempt to write
a book. I would love to have your feedback either appreciation or constructive
criticism, which will help me to write better next time.

CONNECT WITH ME

EMAIL YOUTUBE FACEBOOK INSTAGRAM

thesoftwareguy7@gmail.com /c/thesoftwareguy7 /thesoftwareguy7 /thesoftwareguy7

This book comes bundled with source codes and is available for FREE.
All the Content & Rights is reserved to its author.

SHAHRUKH KHAN www.thesoftwareguy.in 6


100 PATTERN PROGRAMS IN PHP

Important Announcement

Free PHP projects & video tutorials will be available


on my YouTube Channel soon.

Make sure you subscribe my channel & press the bell


icon, to receive all notifications.

Click on the image below

SHAHRUKH KHAN www.thesoftwareguy.in 7


100 PATTERN PROGRAMS IN PHP

Preface
When I was in college, I was hooked coding these pattern programs from square,
pyramid, raindrops, kites, billboards, etc I tried to code them all. Sometimes I
succeeded, on other occasions I failed. I have always wondered why these patterns
programs are so common in all programming languages. What do you get practicing so
many different variations of patterns over and over again? But I didn’t have any clue
back then. I did what I was told by my teacher.

But there was something about those pattern programs that kept us hooked. The time
we invest in coding those programs, printing output, cursing ourselves after finding
silly mistakes, patting on our back after solving a complex pattern. What did we get
from it?

The answer is EXPERIENCE. Solving these complex problems, make us really think.
Which is what programming is all about.

I am writing this book for students who are learning programming languages or want to
learn how to code the pattern program. If you are in college, school or a newbie to
programming, please remember that coding patterns (using loops/control structures)
are the most essential part and you must master it.

I can guarantee you if you give your 100% to learn to code these patterns, you are
surely going to become a very good problem-solving, which is a skill every great
programmer needs to have in his armory.

Although the codes in this book are written in PHP, readers of other programming
languages like Java, Python, etc can also learn the logic from it, because the concept is
same everywhere just the syntax is different.

This book comes in C language version as well. To get a copy click here or visit this link.
https://www.thesoftwareguy.in/100-pattern-programs-in-c-free-ebook

I actually hate programming, but I love solving problems.


- Rasmus Lerdorf – Creator of PHP

SHAHRUKH KHAN www.thesoftwareguy.in 8


100 PATTERN PROGRAMS IN PHP

Resources

Thank you for downloading my book. I am sure you will make the best use of it.

I would like to let you know that this book comes bundled with source codes of 100 programs
in separate text files and arranged in their respective folder.

In case, you got this book from any other source, download the bundle through the link
given below.
https://www.thesoftwareguy.in/100-pattern-programs-in-php-free-ebook

Section Programs Count


Warmup Exercises 05
Geometrical Pattern 30
Numbers Pattern 10
Alphabet Pattern 26
Mixed Sets 29
TOTAL 100

BEFORE GETTING STARTED


I have tested codes on all modern browser and it works fine.
For learners/programmers who have coding experience in programming languages
like C, C++, Java, etc. You guys can learn the code logic from here. IT WORKS 100%
To print star, alphabet or numbers, for pretty formatting I have used additional space
as well, you can omit that if you want. Eg. echo “* ”
So, to adjust two characters (“* ”) with spaces as required I have used it with 3
spaces (“     ”), if you omit the additional space I have used, you
can use 2 spaces (“   ”). You will learn these in upcoming programs.
If you can make any program code logic easier. Please feel free to let me know.

MY RECOMMENDED WAY OF READING


I will recommend you to install PHP in your machine or use any online PHP tool, so
you can test the program while reading.
JUST READING WONT WORK, YOU NEED TO PRACTICE, PRACTICE AND
PRACTICE.

SHAHRUKH KHAN www.thesoftwareguy.in 9


100 PATTERN PROGRAMS IN PHP

A. Warmup Exercises

In this section, you will get started with some of the basic programs to help you get
acquainted with the working of loops.

1. Program to print your name five times

PROGRAM LOGIC

• This is a very straight forward, start a loop and iterate it 5 times.

CODE - STYLE #1

for ($row = 1; $row <= 5; $row++) {


echo 'Shahrukh Khan';
echo '<br>';
}

CODE - STYLE #2

for ($row = 100; $row > 95; $row--) {


echo 'Shahrukh Khan';
echo '<br>';
}

OUTPUT

Shahrukh Khan
Shahrukh Khan
Shahrukh Khan
Shahrukh Khan
Shahrukh Khan

IMPORTANT NOTE
Our objective is to loop around 5 times, now it is our wish how we iterate it, i.e.
increment or decrement wise. So NEVER think as if there’s only a fixed pattern to do
things, as directed in books or by your teacher. Use your creativity.

SHAHRUKH KHAN www.thesoftwareguy.in 10


100 PATTERN PROGRAMS IN PHP

2. Program to print numbers from 1 to 10 but skipping numbers 4 and 6

PROGRAM LOGIC

• I am continuing with the last program


• Start a loop and iterate it 10 times
• Check if the current counter has value 4 or 6, if yes then skip else print the value.

CODE – STYLE #1

for ($row = 1; $row <= 10; $row++) {


if ($row != 4 && $row != 6) {
echo $row;
echo '<br>';
}
}

CODE – STYLE #2

for ($row = 1; $row <= 10; $row++) {


if ($row == 4 || $row == 6)
continue;
echo $row;
echo '<br>';
}

OUTPUT

1
2
3
5
7
8
9
10

SHAHRUKH KHAN www.thesoftwareguy.in 11


100 PATTERN PROGRAMS IN PHP

3. Program to print 4 X 3 grid with hello world

Before starting, it is important that you understand the concept of GRID/MATRIX. If


you failed to understand this, I am Sorry to say, you won’t be able keep up with the
upcoming sophisticated patterns in the book. SO PLEASE FOCUS.

What is a GRID / Matrix? How it works

A matrix is a rectangular array arranged in rows and columns. Given below is a 4X3
(rows x columns) matrix or grid.
B

A C

A – Represent rows
B – Represent Columns
C – Represent Cell

PROGRAM LOGIC

• Start an outer loop (represent rows) that iterate n number of times.


• Under the outer loop, start an inner loop (represent columns), that iterate m
number of times.
• In our case, the outer loop will iterate 4 times and inner loop 3 times,
• But the thing to notice here is that the inner loop gets iterated n x m times i.e. 4 x
3 = 12 times.

CODE

for ($row = 1; $row <= 4; $row++) {


echo 'Hello ' . $row;
for ($column = 1; $column <= 3; $column++) {
echo 'World ' . $column;
}
echo '<br>';
}

OUTPUT

Hello1 World1 World2 World3


Hello2 World1 World2 World3
Hello2 World1 World2 World3
Hello2 World1 World2 World3

SHAHRUKH KHAN www.thesoftwareguy.in 12


100 PATTERN PROGRAMS IN PHP

4. Program to print 3 X 5 table dynamically in PHP

RAW HTML TABLE

<table>
<tr>
<td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>
</tr>
<tr>
<td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>
</tr>
<tr>
<td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>
</tr>
</table>

PROGRAM LOGIC

• Initialize a variable with empty string.


• Append <table> tag to the variable.
• Start outer loop (rows) that iterate 3 times, append a <tr> tag to the variable .
• Under the outer loop, start and inner loop (columns) that iterate 5 times, append
<td> tag to the variable.
• Close <table> tag & print the variable.

CODE

$str = '';
$str .= '<table border="1">';
for ($row = 1; $row <= 3; $row++) {
$str .= '<tr>';
for ($column = 1; $column <= 5; $column++) {
$str .= '<td>'.$column.'</td>';
}
$str .= '</tr>';
}
$str .= '</table>';
echo $str;

OUTPUT

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

SHAHRUKH KHAN www.thesoftwareguy.in 13


100 PATTERN PROGRAMS IN PHP

5. Program to print table from 1 to 10

OUTPUT ON SCREEN
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• Start an outer loop (columns) that iterate 10 times.
• Under the outer loop start an inner loop (column), that iterate 10 times.
• Just multiply the current row value with the current column value inside the
inner loop.

CODE

$row_length = 10; $column_length = 10;


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
echo ($row * $column ) . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 14


100 PATTERN PROGRAMS IN PHP

B. Geometrical Pattern

In this section, you will learn to print different geometrical shapes like a pyramid, right-
angle triangle, reverse pyramid, inverted pyramid, diamond, rhombus, rectangle,
hollow diamonds, etc and their different variations with stars, numbers and alphabet.

Please understand, that every shape is nothing but a pattern inside the grid marked
with either space or character. So, always think the shapes in rows and columns rather
as a whole.

There are 30 programs in this section, I have arranged them in such an order so that it
is easy for you to understand. After about 10-12 programs if you still don’t understand,
I will suggest you go back to program 1 and start again.

IMPORTANT NOTE
Before diving into this section, I will recommend you to practice Section A programs
with sincerity. Try to play with them and shuffle variables and numbers. It will help
boost your confidence.

SHAHRUKH KHAN www.thesoftwareguy.in 15


100 PATTERN PROGRAMS IN PHP

1. Program to print square with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


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

PROGRAM LOGIC

• Take 5 rows and 5 columns.


• Start an outer loop (row) that will iterate 5 times.
• Under the outer loop start an inner loop (column) that iterate the same number
of times
• Inside the inner loop print star.

CODE

$row_length = 5; $column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 16


100 PATTERN PROGRAMS IN PHP

2. Program to print square with number pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 1 2 3 4 5

PROGRAM LOGIC

• The logic is same as program B1


• Print current column value instead of star.

CODE

$row_length = 5; $column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
echo $column."&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 17


100 PATTERN PROGRAMS IN PHP

3. Program to print square with number pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5

PROGRAM LOGIC

• The logic is same as program B1


• Print current row value instead of star.

CODE

$row_length = 5; $column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
echo $row."&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 18


100 PATTERN PROGRAMS IN PHP

4. Program to print square with alphabet pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


A A A A A 65 65 65 65 65
B B B B B 66 66 66 66 66
C C C C C 67 67 67 67 67
D D D D D 68 68 68 68 68
E E E E E 69 69 69 69 69

PROGRAM LOGIC

• The logic is same as program B1


• The ASCII code for capital A is 65 and small a is 97. Use whichever you prefer.
• Set a variable $ascii_code_start to 65 and increment by 1 after each row
iteration.
• Use chr() function to print the character.

CODE

$row_length = 5; $column_length = 5;
$ascii_code_start = 65;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
echo chr($ascii_code_start) . "&nbsp;";
}
$ascii_code_start++;
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 19


100 PATTERN PROGRAMS IN PHP

5. Program to print square with alphabet pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


A B C D E 65 66 67 68 69
B C D E F 66 67 68 69 70
C D E F G 67 68 69 70 71
D E F G H 68 69 70 71 72
E F G H I 69 70 71 72 73

PROGRAM LOGIC

• The logic is same as program B1


• Inside the inner loop add $ascii_code_start variable to current row to current
column, use this condition ($ascii_code_start + $row + $colum)

CODE

$row_length = 5; $column_length = 5;
$ascii_code_start = 65;

for ($row = 0; $row < $row_length; $row++) {


for ($column = 0; $column < $column_length; $column++) {
$print_value = ($ascii_code_start + $row) + $column;
echo chr($print_value) . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 20


100 PATTERN PROGRAMS IN PHP

6. Program to print hollow square with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


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

PROGRAM LOGIC

• The logic is same as program B1


• On the first and last row (marked in green), print star in all columns, use this
condition ($row == 1 || $row == $row_length)
• From second till second-to-last row (marked in red), print star in the first and
last column only, use this condition ($column == 1 || $column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 5; $column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if($row == 1 || $row == $row_length) {
echo "*&nbsp;";
} else if($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 21


100 PATTERN PROGRAMS IN PHP

7. Program to print rectangle shape with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


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

PROGRAM LOGIC

• Take 5 rows and 10 columns.


• Start an outer loop (row) that will iterate 5 times.
• Under the outer loop start an inner loop (column) that iterate the 10 times.
• Inside the inner loop print star.

CODE

$row_length = 5; $column_length = 10;


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 22


100 PATTERN PROGRAMS IN PHP

8. Program to print hollow rectangle with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


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

PROGRAM LOGIC

• The logic is same as program B7


• On the first and last row (marked in green), print star in all columns, use this
condition ($row == 1 || $row == $row_length)
• From second till second-to-last row (marked in red), print star in the first and
last column only, use this condition ($column == 1 || $column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 5; $column_length = 10;


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 23


100 PATTERN PROGRAMS IN PHP

9. Program to print right-angle triangle with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• Observe, as the rows increase column value increases. See Row 1 has 1 star,
Row 2 has 2 stars.
• Start an outer loop (row) that will iterate 10 times.
• Under the outer loop start an inner loop (column) that iterate the same number
of times
• Inside the inner loop print star.

CODE

$row_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 24


100 PATTERN PROGRAMS IN PHP

10. Program to print right-angle triangle with number pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
1 2 1 2
1 2 3 1 2 3
1 2 3 4 1 2 3 4
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 6 1 2 3 4 5 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

PROGRAM LOGIC

• The logic is same as program B9


• Print the current column value instead of star.

CODE

$row_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
echo $column."&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 25


100 PATTERN PROGRAMS IN PHP

11. Program to print inverted right-angle triangle with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• Observe, as the rows increase column value decreases. See Row 1 has 10 stars,
Row 2 has 9 stars.
• Start an outer loop (row) that will iterate 10 times.
• Under the outer loop start an inner loop (column) that iterate till row length
minus current row value.
• Inside the inner loop print star.

CODE

$row_length = 10;
for ($row = 0; $row < $row_length; $row++) {
for ($column = 0; $column < $row_length - $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 26


100 PATTERN PROGRAMS IN PHP

12. Program to print inverted right-angle triangle with number pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7
8 8 8 8 8 8
9 9 9 9
10 10

PROGRAM LOGIC

• The logic is same as program B11


• Initialize a counter variable with 1.
• Print the counter value inside inner loop.
• After each row iteration, increment counter by 1.

CODE

$row_length = 10; $counter = 1;


for ($row = 0; $row < $row_length; $row++) {
for ($column = 0; $column < $row_length - $row; $column++) {
echo $counter . "&nbsp;";
}
$counter++;
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 27


100 PATTERN PROGRAMS IN PHP

13. Program to print mirror right-angle triangle with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• The blank spaces in the grid (marked in yellow) will come in play here.
• Observe, as rows increase, space decreases. Row 1 has 9 spaces; Row 2 has 8
spaces.
• Again, as rows increases column (star) value increases. Row 1 has 1 stars; Row 2
has 2 stars.
• Start an outer loop (row) that will iterate 10 times.
• Under the outer loop, start an inner loop (for spaces) that iterate till total row
length minus current row value.
• After the inner loop for spaces, start loop again (for star) that iterate till current
row value. Print star inside this loop.

CODE

$row_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 28


100 PATTERN PROGRAMS IN PHP

14. Program to print mirror right-angle triangle with number pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
1 2 1 2
1 2 3 1 2 3
1 2 3 4 1 2 3 4
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 6 1 2 3 4 5 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

PROGRAM LOGIC

• The logic is same as program B13


• Print the current column value instead of star.

CODE

$row_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
echo $column."&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 29


100 PATTERN PROGRAMS IN PHP

15. Program to print inverted mirror right-angle triangle with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• The blank spaces in the grid (marked in yellow) will come in play here.
• Observe, as rows increase, space increases. Row 1 has 0 space; Row 2 has 1
space.
• Again, as rows increases column (star) value decreases. Row 1 has 10 stars; Row
2 has 9 stars.
• Start an outer loop (row) that will iterate 10 times.
• Under the outer loop, start an inner loop (for spaces) that iterate till current row
value.
• After the inner loop for spaces, start loop again (for star) that iterate till total
row length minus current row value. Print star inside this loop.

CODE

$row_length = 10;
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 0; $column < $row_length - $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 30


100 PATTERN PROGRAMS IN PHP

16. Program to print inverted hollow mirror right-angle triangle with star
pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B15


• On the first and last row (marked in red), print star in all columns, use this
condition ($row ==0)
• From second till last row (marked in green), print star in the current column that
matches current row and last column, use this condition ($column == 0 ||
$column == ($row_length - $row)-1)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 0; $column < $row_length - $row; $column++) {
if ($row == 0) {
echo "*&nbsp;";
} else if ($column == 0 || $column == ($row_length - $row)-1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 31


100 PATTERN PROGRAMS IN PHP

17. Program to print hollow right-angle triangle with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B9


• From first till second-to-last row (marked in green), print star in the first column
and current column that matches current row, use this condition ($column == 1
|| $column == $row)
• On the last row (marked in red), print star in all columns, use this condition ($row
==$row_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
if ($column == 1 || $column == $row ) {
echo "*&nbsp;";
} else if ($row == $row_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}

}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 32


100 PATTERN PROGRAMS IN PHP

18. Program to print hollow inverted right-angle triangle with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B11


• On the first row (marked in red), print star in all columns, use this condition
($row ==0)
• From second till last row (marked in green), print star in the first column and
current column that matches current row, use this condition ($column == 0 ||
$column == ($row_length - $row)-1)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
for ($row = 0; $row < $row_length; $row++) {
for ($column = 0; $column < $row_length - $row; $column++) {
if ($row == 0) {
echo "*&nbsp;";
} else if($column == 0 || $column == ($row_length - $row)-1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 33


100 PATTERN PROGRAMS IN PHP

19. Program to print hollow mirror right angle triangle with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B13


• From first till second-to-last row (marked in green), print star in the current
column that matches current row and last row, use this condition ($column == 1
|| $column == $row)
• On the last row (marked in red), print star in all columns, use this condition ($row
==$row_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
if ($column == 1 || $column == $row) {
echo "*&nbsp;";
} else if ($row == $row_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 34


100 PATTERN PROGRAMS IN PHP

20. Program to print rhombus with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 5 rows and 10 columns.


• The blank spaces in the grid (marked in yellow) will come in play here.
• Observe, as rows increase, space decreases. Row 1 has 5 spaces; Row 2 has 4
spaces.
• Start an outer loop (row) that will iterate 5 times.
• Under the outer loop, start an inner loop (for spaces) that iterate till total row
length minus current row value.
• After the inner loop for spaces, start loop again (for star) that iterate 5 times.
Print star inside this loop.

CODE

$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 1; $column <= $row_length; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 35


100 PATTERN PROGRAMS IN PHP

21. Program to print hollow rhombus with star

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B20


• On the first and last row (marked in green), print star in all columns, use this
condition ($row == 0 || $row == $row_length-1)
• From second till second-to-last row (marked in red), print star in the first and
last column only, use this condition ($column == 1 || $column == $row_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 1; $column <= $row_length; $column++) {
if ($column == 1 || $column == $row_length) {
echo "*&nbsp;";
} else if ($row == 0 || $row == $row_length - 1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 36


100 PATTERN PROGRAMS IN PHP

22. Program to print pyramid with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B13. The trick here is playing with spaces.
• Observe, as rows increases spaces decreases (marked in yellow). Row 1 has 10
stars; Row 2 has 9 stars.
• Again, as the rows increase column (stars) increases. See Row 1 has 1 star, Row 2
has 2 stars.
• Start an outer loop (row) that will iterate 5 times.
• Under the outer loop, start an inner loop (for spaces) that iterate till total row
length minus current row value.
• After the inner loop for spaces, start loop again (for star) that iterate till current
row value. Print star inside this loop.

CODE

$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 37


100 PATTERN PROGRAMS IN PHP

23. Program to print inverted pyramid with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B22, switch the numbers to make it upside-down.
• Start an outer loop (row) that will iterate 5 times.
• Under the outer loop, start an inner loop (for spaces) that iterate till current row
value.
• After the inner loop for spaces, start loop again (for star) that iterate till total
row length minus current row value. Print star inside this loop.

CODE

$row_length = 10;
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 0; $column < $row_length - $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 38


100 PATTERN PROGRAMS IN PHP

24. Program to print hollow pyramid with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B22


• From first till second-to-last row (marked in green), print star in the first row and
current column that matches current row, use this condition ($column == 1 ||
$column == $row)
• On the last row (marked in red), print star in all columns, use this condition ($row
==$row_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
if ($column == 1 || $column == $row) {
echo "*&nbsp;";
} else if ($row == $row_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 39


100 PATTERN PROGRAMS IN PHP

25. Program to print inverted hollow pyramid with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program B23, switch the numbers to make it upside-down.
• On the first row (marked in red), print star in all columns, use this condition
($row ==0)
• From second till last row (marked in green), print star in the first row and current
column that matches current row, use this condition ($column == 0 || $column
== ($row_length - $row)-1)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 0; $column < $row_length - $row; $column++) {
if ($row == 0) {
echo "*&nbsp;";
} else if ($column == 0 || $column == ($row_length - $row)-1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 40


100 PATTERN PROGRAMS IN PHP

26. Program to print acute triangle with star pattern – style 1

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• In the upper section (marked in green) logic is same as the program B9


• In the lower section (marked in red) logic is same as the program B11

CODE

$row_length = 5;
// top section
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}
// bottom section
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row_length-$row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 41


100 PATTERN PROGRAMS IN PHP

27. Program to print acute triangle with star pattern – style 2

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• In the upper section (marked in green) logic is same as the program B13
• In the lower section (marked in red) logic is same as the program B15

CODE

$row_length = 5;
// upper section
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}
// bottom section
for ($row = 1; $row < $row_length; $row++) {
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 0; $column < $row_length - $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 42


100 PATTERN PROGRAMS IN PHP

28. Program to print hollow diamond with star

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• This program is a combination of multiple sub program covered so far.


• In the upper section (marked in green) is a combination of program B11 & B15
• In the lower section (marked in red) is a combination of program B9 & B13

CODE

$row_length = 5;

// upper section
for ($row = 0; $row < $row_length; $row++) {
//left section
for ($column = 1; $column <= $row_length - $row; $column++) {
echo "*&nbsp;";
}

for ($spaces = 0; $spaces < $row; $spaces++) {


echo "&nbsp;&nbsp;&nbsp;";
}

// right section
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = $row_length - $row; $column >= 1; $column--) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 43


100 PATTERN PROGRAMS IN PHP

// lower section
for ($row = 2; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}

for ($spaces = 0; $spaces < $row_length - $row; $spaces++) {


echo "&nbsp;&nbsp;&nbsp;";
}

for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {


echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 44


100 PATTERN PROGRAMS IN PHP

29. Program to print hourglass with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• In the upper section (marked in green) logic is same as the program B23
• In the lower section (marked in red) logic is same as the program B22

CODE

$row_length = 5;
// upper section
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 0; $column < $row_length - $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}
//Lower section
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 45


100 PATTERN PROGRAMS IN PHP

30. Program to print hollow diamond with star

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• This program is a combination of two programs.


• In the upper section (marked in green) logic is same as the program B24
• In the lower section (marked in red) logic is same as the program B25

CODE

$row_length = 5;

// upper section
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
if ($column == 1 || $column == $row) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;";
}
}
echo '<br>';
}

// lower section
for ($row = 1; $row < $row_length; $row++) {
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 0; $column < $row_length - $row; $column++) {

SHAHRUKH KHAN www.thesoftwareguy.in 46


100 PATTERN PROGRAMS IN PHP

if ($row == ($row_length - 1)) {


echo "*&nbsp;";
} else if ($column == 0 || $column == ($row_length - $row) - 1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 47


100 PATTERN PROGRAMS IN PHP

C. Numbers Pattern

In this section, you will learn to print all the number from 0 to 9 with star pattern.

I am going to print those number with star pattern as closely as possible. If you think
you can have a better version of the code, please do notify me.

There are 10 programs in this section. I recommend not to skip this section, because
this will help you learn the basics of printing alphabet with star pattern.

IMPORTANT NOTE
Before diving into this section. I am assuming you have finished learning Section B.
If not please go back to Section B.

SHAHRUKH KHAN www.thesoftwareguy.in 48


100 PATTERN PROGRAMS IN PHP

1. Program to print number one with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


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

PROGRAM LOGIC

• Take 10 rows and 11 columns, for the 6th column will be the exact middle.
• On the first row (marked with green), print star from first till middle column, use
this condition ($row == 1 && $column < $column_middle)
• From second till second-to-last row (marked with red), print star at the middle
column, use this condition ($column == $column_middle)
• Finally, on the last row (marked with blue) print star in all columns, use this
condition ($row == $row_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 11; $column_middle = ceil($column_length / 2);


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 && $column < $column_middle) {
echo "*&nbsp;";
} else if ($column == $column_middle) {
echo "*&nbsp;";
} else if ($row == $row_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 49


100 PATTERN PROGRAMS IN PHP

2. Program to print number two with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first, middle and last row (marked with green), print star in all columns,
use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
• From second till middle row (marked with red), print star at the last column, use
this condition ($column == $column_length && $row < $row_middle)
• Also, from middle to second-to-last row (marked with blue) print star at the first
column, use this condition ($column == 1 && $row > $row_middle)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11;$column_length = 10; $row_middle = ceil($row_length / 2);


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == $column_length && $row < $row_middle) {
echo "*&nbsp;";
} else if ($column == 1 && $row > $row_middle) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 50


100 PATTERN PROGRAMS IN PHP

3. Program to print number three with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first, middle and second-to-last row (marked with green), print star in all
columns, use this condition ($row == 1 || $row == $row_middle || $row ==
$row_length-1)
• From second till second-to-last row (marked with red), print star at the last
column, use this condition ($column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length-1 ) {
echo "*&nbsp;";
} else if ($column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 51


100 PATTERN PROGRAMS IN PHP

4. Program to print number four with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first column (marked with red), print star from first till middle row, use
this condition ($column == 1 && $row <= $row_middle)
• At the mid row (marked with blue), print star in all columns, use this condition
($row == $row_middle)
• Finally, on the last column (marked with green) print star in all row, use this
condition ($column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($column == 1 && $row <= $row_middle) {
echo "*&nbsp;";
} else if ($row == $row_middle) {
echo "*&nbsp;";
} else if ($column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 52


100 PATTERN PROGRAMS IN PHP

5. Program to print number five with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as program C2, just play with column arrangements.
• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first, middle and last row (marked with green), print star in all columns,
use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
• From second till middle row (marked with blue), print star at the first column,
use this condition ($column == 1 && $row < $row_middle)
• Also, from middle to second-to-last row (marked with red) print star at the first
column, use this condition ($column == $column_length && $row > $row_middle)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 && $row < $row_middle) {
echo "*&nbsp;";
} else if ($column == $column_length && $row >= $row_middle) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 53


100 PATTERN PROGRAMS IN PHP

6. Program to print number six with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first, middle and last row (marked with green), print star in all columns,
use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
• For first column (marked with red), print star in each row, use this condition
($column == 1)
• Finally, from middle till second-to-last row (marked with blue), print star at the
last column, use this condition ($column == $column_length && $row >
$row_middle)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10; $row_middle = ceil($row_length / 2);


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1) {
echo "*&nbsp;";
} else if ($column == $column_length && $row > $row_middle) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 54


100 PATTERN PROGRAMS IN PHP

SHAHRUKH KHAN www.thesoftwareguy.in 55


100 PATTERN PROGRAMS IN PHP

7. Program to print number seven with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns as there is no mid-section calculation.


• On the first row (marked with green), print star in all columns, use this condition
($row == 1)
• For last column (marked with red), print star in each row, use this condition
($column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 10;


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1) {
echo "*&nbsp;";
} else if ($column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 56


100 PATTERN PROGRAMS IN PHP

8. Program to print number eight with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first, middle and second-to-last row (marked with green), print star in all
columns, use this condition ($row == 1 || $row == $row_middle || $row ==
$row_length-1)
• From second till second-to-last row (marked with red), print star at the first and
last column, use this condition ($column == 1 || $column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length ) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 57


100 PATTERN PROGRAMS IN PHP

9. Program to print number nine with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first, middle and last row (marked with green), print star in all columns,
use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
• From first till middle column (marked with red), print star in first column, use this
condition ($column == 1 && $row < $row_middle)
• Finally, on the last column (marked with blue), print star at each row, use this
condition ($column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 && $row < $row_middle) {
echo "*&nbsp;";
} else if ($column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 58


100 PATTERN PROGRAMS IN PHP

10. Program to print number zero with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• This pattern is similar to hollow square, check the logic from the program B6

CODE

$row_length = 10; $column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {


for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $row_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 59


100 PATTERN PROGRAMS IN PHP

D. Alphabet Pattern

In this section, you will learn to print letters from A to Z

I hope you had a great time solving patterns on numbers and geometrical shape.

There are 26 programs in this section, if you have solved the numbers pattern then I
am sure you won’t face trouble solving these problems.

IMPORTANT NOTE
Before diving into this section. I am assuming you have finished learning Section B &
Section C. If not please go back and read.

SHAHRUKH KHAN www.thesoftwareguy.in 60


100 PATTERN PROGRAMS IN PHP

1. Program to print letter A with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first and middle row (marked with green), print star in all columns, use
this condition ($row == 1 || $row == $row_middle)
• Print star at the first and last column of each row (marked with red), use this
condition ($column == 1 || $column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10; $row_middle = ceil($row_length / 2);


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 61


100 PATTERN PROGRAMS IN PHP

2. Program to print letter B with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first, middle and last row (marked with green), print star in all columns,
use this condition ($row == 1 || $row == $row_middle || $row == $row_middle)
• Print star at the second and last column of each row (marked with red), use this
condition ($column == 2 || $column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length ) {
echo "*&nbsp;";
} else if ($column == 2 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 62


100 PATTERN PROGRAMS IN PHP

3. Program to print letter C with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• On the first and last row (marked with green), print star in all columns, use this
condition ($row == 1 || $row == $row_middle)
• Print star at the first column of each row (marked with red), use this condition
($column == 1)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length ) {
echo "*&nbsp;";
} else if ($column == 1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 63


100 PATTERN PROGRAMS IN PHP

4. Program to print letter D with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• On the first and last row (marked with green), print star in all columns, use this
condition ($row == 1 || $row == $row_middle)
• Print star at the second and last column of each row (marked with red), use this
condition ($column == 2 || $column == $column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
$column_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length ) {
echo "*&nbsp;";
} else if ($column == 2 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 64


100 PATTERN PROGRAMS IN PHP

5. Program to print letter E with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first, middle and last row (marked with green), print star in all columns,
use this condition ($row == 1 || $row == $row_middle || $row == $row_middle)
• Print star at the first column of each row (marked with red), use this condition
($column == 1)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length ) {
echo "*&nbsp;";
} else if ($column == 1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 65


100 PATTERN PROGRAMS IN PHP

6. Program to print letter F with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first and middle row (marked with green), print star in all columns, use
this condition ($row == 1 || $row == $row_middle)
• Print star at the first column of each row (marked with red), use this condition
($column == 1)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {


for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle ) {
echo "*&nbsp;";
} else if ($column == 1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 66


100 PATTERN PROGRAMS IN PHP

7. Program to print letter G with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first and last row (marked with green), print star in all columns, use this
condition ($row == 1 || $row == $row_length)
• Print star on the first column of each row (marked with red), use this condition
($column == 1)
• Finally, from the middle column till second-to-last column (marked with blue),
print star in last column, use this condition ($column == column_length && $row
>= $row_middle)
• Finally, on the last column, print star at each row, use this condition ($column ==
$column_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1) {
echo "*&nbsp;";
} else if ($column == $column_length && $row >= $row_middle) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}

SHAHRUKH KHAN www.thesoftwareguy.in 67


100 PATTERN PROGRAMS IN PHP

echo "<br>";
}

8. Program to print letter H with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for 6th row will be the exact middle.
• Print star at the first and last column of each row (marked with green), use this
condition ($column == 1 || $column == $column_length)
• On the middle row (marked with red), print star in all columns, use this condition
($row == $row_middle)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {


for ($column = 1; $column <= $column_length; $column++) {
if ($row == $row_middle ) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length ) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 68


100 PATTERN PROGRAMS IN PHP

SHAHRUKH KHAN www.thesoftwareguy.in 69


100 PATTERN PROGRAMS IN PHP

9. Program to print letter I with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 11 columns, for 6th column will be the exact middle.
• On the first and last row (marked with red), print star in all columns, use this
condition ($row == 1 || $row == $row_middle)
• Print star at the middle column of each row (marked with green), use this
condition ($column == $column_middle)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 11;


$column_middle = ceil($column_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length ) {
echo "*&nbsp;";
} else if ($column == $column_middle) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 70


100 PATTERN PROGRAMS IN PHP

10. Program to print letter J with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 11 columns, for the 6th column will be the exact middle.
• On the first row (marked with green), print star in all columns, use this condition
($row == 1)
• Print star at the middle column of each row (marked with red), use this condition
($column == $column_middle)
• On the last row, print star from first till middle column (marked with blue), use
this condition ($column < $column_middle && $row == $row_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 11;


$column_middle = ceil($column_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 ) {
echo "*&nbsp;";
} else if ($column == $column_middle) {
echo "*&nbsp;";
} else if ($column < $column_middle && $row == $row_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 71


100 PATTERN PROGRAMS IN PHP

11. Program to print letter K with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Program in divided into two parts.


• Take 10 rows and 5 columns. 5X5 grid twice.
• For upper section (marked in green), print star on first column, then spaces and
then star. Observe as row increases, spaces between stars decrease. Use this
condition ($column <= ($row_length - $row))
• For lower section (marked in red), print star on first column, then spaces and
then star. Observe as row increases, spaces between stars also increase. Use this
condition ($column == $row)

CODE

$row_length = 5; $column_length = 5;

// Top section starts


for ($row = 1; $row <= $row_length; $row++) {
echo "*&nbsp;";
for ($column = 1; $column <= $column_length; $column++) {
if ($column <= ($row_length - $row)) {
echo "&nbsp;&nbsp;&nbsp;";
} else {
echo "*&nbsp;";
break;
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 72


100 PATTERN PROGRAMS IN PHP

// Bottom section starts


for ($row = 1; $row <= $row_length; $row++) {
echo "*&nbsp;";
for ($column = 1; $column <= $column_length; $column++) {

if ($column == $row) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}

echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 73


100 PATTERN PROGRAMS IN PHP

12. Program to print letter L with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• Print star at the first column of each row (marked with green, use this condition
($column == 1)
• For last column (marked with red), print star in all columns, use this condition
($row == $row_length)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {


for ($column = 1; $column <= $column_length; $column++) {
if ($row == $row_length ) {
echo "*&nbsp;";
} else if ($column == 1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 74


100 PATTERN PROGRAMS IN PHP

13. Program to print letter M with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 11 columns, for 6th column will be the exact middle.
• Print star at the first & last column of each row (marked with green), use this
condition ($column == 1 || $column == $column_length)
• On the last row (marked with red), print star in all columns, use this condition
($row == 1)
• From first till middle row (marked with blue), print star in middle column, use
this condition ($column == $column_middle && $row < $row_length / 2)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 11;


$column_middle = ceil($column_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else if ($column == $column_middle && $row <= ($row_length/2)) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 75


100 PATTERN PROGRAMS IN PHP

14. Program to print letter N with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• Print star at the first & last column of each row (marked with green), use this
condition (column == 1 || column == column_length)
• From first till second-to-last column (marked in red), print star where current
column that matches current row, use this condition (column == row)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 10;


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {

if ($column == 1 || $column == $column_length) {


echo "*&nbsp;";
} else if ($column == $row) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 76


100 PATTERN PROGRAMS IN PHP

15. Program to print letter O with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• This pattern is similar to hollow square, check the logic from the program B6

CODE

$row_length = 10; $column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {


for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 77


100 PATTERN PROGRAMS IN PHP

16. Program to print letter P with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 11 rows and 10 columns, for the 6th row will be the exact middle.
• On the first and last row (marked with green), print star in all columns, use this
condition ($row == 1 && $row == $row_middle)
• From second till middle row (marked with blue), print star at the last column, use
this condition ($column == $column_length && $row < $row_middle)
• Print star at the first column of each row (marked with red), use this condition
($column == 1)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle) {
echo "*&nbsp;";
} else if ($column == 1) {
echo "*&nbsp;";
} else if ($column == $column_length && $row < $row_middle) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 78


100 PATTERN PROGRAMS IN PHP

17. Program to print letter Q with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns, and 5th row is the mid-section here.
• On the first and last row (marked with green), print star in all columns, use this
condition ($row == 1 && $row == $row_middle)
• Print star at the first and last column of each row (marked with red), use this
condition ($column == 1 && $column == $column_length)
• Finally, for the inner part, print star from mid row till second-to-last row where
current column is equals to current row (marked with blue), use this condition
($row >= $row_middle && $column == $row)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 10; $row_middle = ceil($row_length / 2);


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "* ";
} else if ($row >= $row_middle && $column == $row) {
echo "* ";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 79


100 PATTERN PROGRAMS IN PHP

18. Program to print letter R with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The program is divided into two parts.


• The upper section (marked in green) is like letter O, logic is same as program B6
• The lower section (marked in red) is like the lower section of letter K, logic is
same as program B16

CODE

$row_length = 5; $column_length = 5;

// upper section
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}
// bottom section
for ($row = 1; $row <= $row_length; $row++) {
echo "*&nbsp;";
for ($column = 1; $column <= $column_length; $column++) {

if ($column == $row ) {

SHAHRUKH KHAN www.thesoftwareguy.in 80


100 PATTERN PROGRAMS IN PHP

echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}

echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 81


100 PATTERN PROGRAMS IN PHP

19. Program to print letter S with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The logic is same as the program C5

CODE

$row_length = 11; $column_length = 10;


$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_middle || $row == $row_length ) {
echo "*&nbsp;";
} else if ($column == 1 && $row < $row_middle) {
echo "*&nbsp;";
} else if ($column == $column_length && $row > $row_middle) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 82


100 PATTERN PROGRAMS IN PHP

20. Program to print letter T with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 11 columns, for 6th column will be the exact middle.
• On the first row (marked in green), print star in all columns, use this condition
($row == 1)
• From second till last row (marked in red), print star in the middle column only,
use this condition ($column == $column_middle)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 11;


$column_middle = ceil($column_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1) {
echo "*&nbsp;";
} else if ($column == $column_middle) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 83


100 PATTERN PROGRAMS IN PHP

21. Program to print letter U with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• Print star at the first and last column of each row (marked with green), use this
condition ($column == 1 || $column == $column_length)
• On the last row (marked with red), print star in all columns, use this condition
($row == $row_middle)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 10;


for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ( $row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 84


100 PATTERN PROGRAMS IN PHP

22. Program to print letter V with star pattern

VERSION 1

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 5 rows and 5 columns (loop twice, side by side).


• Start an outer loop (row) that will iterate 5 times.
• For first section (marked in green), it’s a combination of spaces and star.
• As row increases spaces increase, after that print star when column number is
equals to current row, use this condition ($column == $row).
• For second section (marked in red), again a combination of spaces and star.
• As row increases spaces decrease, after that print star when current column is
equals to current row, use this condition ($column == $column_length+1).
• If the condition does not satisfy, print spaces.

CODE

$row_length = 5; $column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($column == $row) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
for ($column = 1; $column <= $column_length; $column++) {
if ($column == ($column_length-$row)+1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 85


100 PATTERN PROGRAMS IN PHP

VERSION 2

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

CODE

$row_length = 5; $column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($column == $row) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}

for ($column = 1; $column <= $column_length; $column++) {


// adding +1 for a pointed lower design
if ($column == ($column_length-$row)+1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 86


100 PATTERN PROGRAMS IN PHP

23. Program to print letter W with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• It’s like a reversed letter M


• Take 10 rows and 11 columns, for 6th column will be the exact middle.
• Print star at the first & last column of each row (marked with green), use this
condition ($column == 1 || $column == $column_length)
• On the last row (marked with red), print star in all columns, use this condition
($row == $row_length)
• From middle till last row (marked with blue), print star in middle column, use this
condition ($column == $column_middle && $row > $row_length / 2)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10; $column_length = 11;


$column_middle = ceil($column_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == $row_length) {
echo "*&nbsp;";
} else if ($column == 1 || $column == $column_length) {
echo "*&nbsp;";
} else if ($column == $column_middle && $row >= ($row_length/2)) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 87


100 PATTERN PROGRAMS IN PHP

24. Program to print letter X with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The program is divided into two parts.


• The upper section (marked in green), logic is same as letter V, program D22
• For lower section (marked in red), it’s like inverted letter V.

CODE

$row_length = 5; $column_length = 5;
// upper section
for ($row = 1; $row <= $row_length; $row++) {

for ($column = 1; $column <= $column_length; $column++) {

if ($column == $row) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}

for ($column = 1; $column <= $column_length; $column++) {


// remove +1 if you dont one a flat lower design
if ($column == ($column_length-$row)+1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}

SHAHRUKH KHAN www.thesoftwareguy.in 88


100 PATTERN PROGRAMS IN PHP

echo "<br>";
}

// lower section
for ($row = 1; $row <= $row_length; $row++) {

for ($column = 1; $column <= $column_length; $column++) {


// remove +1 if you dont one a flat lower design
if ($column == ($column_length-$row)+1) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
for ($column = 1; $column <= $column_length; $column++) {

if ($column == $row) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}

echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 89


100 PATTERN PROGRAMS IN PHP

25. Program to print letter Y with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The program is divided into two parts.


• For upper section (marked in green), logic is same as letter V, program D22
• For lower section (marked in red), print stars at the last column, use this
condition ($column == $column_length)

CODE

$row_length = 5;
$column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {

//top section
for ($column = 1; $column <= $column_length; $column++) {

if ($column == $row) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}

for ($column = 1; $column <= $column_length; $column++) {


if ($column == ($column_length - $row)) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";

SHAHRUKH KHAN www.thesoftwareguy.in 90


100 PATTERN PROGRAMS IN PHP

//bottom section
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($column == $column_length) {
echo "*&nbsp;";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 91


100 PATTERN PROGRAMS IN PHP

26. Program to print letter Z with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• Take 10 rows and 10 columns.


• On the first and last row (marked with green), print star in all columns, use this
condition ($row == 1 || $row == $row_length)
• From second till second-to-last column (marked in red), print star where star in a
descending order, use this condition (column <= $row_length-$row)
• If the condition does not satisfy, print spaces.

CODE

$row_length = 10;
$column_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $column_length; $column++) {
if ($row == 1 || $row == $row_length) {
echo "*&nbsp;";
} else if ($column <= ($row_length - $row)) {
echo "&nbsp;&nbsp;&nbsp;";
} else {
echo "*&nbsp;";
break;
}
}
echo "<br>";
}

SHAHRUKH KHAN www.thesoftwareguy.in 92


100 PATTERN PROGRAMS IN PHP

E. Mixed Sets

In this section, you will learn to code different patterns based on what you have learnt
so far. I am sure that after a couple of programs in this section, you will surely able to
grasp the concept and easily understand the program.

There are 29 programs in this section. The programs are in random order.

IMPORTANT NOTE
I am assuming that you have covered all the previous sections and now you are ready
to Rock’N’Roll with the upcoming programs.

SHAHRUKH KHAN www.thesoftwareguy.in 93


100 PATTERN PROGRAMS IN PHP

1. Program to print HTML table maintaining proper tags under given


conditions

HTML Table: Case 1: user input = 16

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

HTML Table: Case 2 user input = 18


1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18

PROGRAM LOGIC

• The maximum number of columns allowed in one row is 4.


• Take user input in a variable. In our case it is $user_input
• Then calculate total number of rows. Use this $no_of_rows = $user_input /
$columns_per_row;
• Then calculate remainder columns $no_of_remaining_col = $user_input %
$columns_per_row;
• In HTML table, rows represent table rows <tr> and column represent <td>
• Start the outer loop (rows), open a <tr> tag and at the end of the loop close the
</tr> tag.
• Under the outer loop, start an inner loop and print the number within <td> tags.
• If $no_of_remaining_col variable is greater than zero then again start an inner
loop to complete the remaining columns.

CODE

$columns_per_row = 4; $user_input = 17;


$no_of_rows = $user_input / $columns_per_row;
$no_of_remaining_col = $user_input % $columns_per_row;
$str = '';
$str .= '<table border="1">';
for ($row = 1; $row <= $no_of_rows; $row++) {
$str .= '<tr>';
for ($column = 1; $column <= $columns_per_row; $column++) {
$str .= '<td>' . $column . '</td>';
}
$str .= '</tr>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 94


100 PATTERN PROGRAMS IN PHP

// if remainder is greater than zero


if ($no_of_remaining_col > 0) {
$str .= '<tr>';
for ($column = 1; $column <= $no_of_remaining_col; $column++) {
$str .= '<td>' . $column . '</td>';
}
$str .= '</tr>';
}

$str .= '</table>';
echo $str;

SHAHRUKH KHAN www.thesoftwareguy.in 95


100 PATTERN PROGRAMS IN PHP

2. Program to print Floyd triangle

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
2 3 +1 +1
4 5 6 +1 +1 +1
7 8 9 10 +1 +1 +1 +1
11 12 13 14 15 +1 +1 +1 +1 +1

PROGRAM LOGIC

• The logic is same as program B9


• Observe, at each iteration value increments by 1.
• Use a counter that increments by 1 inside inner loop.

CODE

$row_length = 5;
$counter = 1;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
echo $counter."&nbsp;";
$counter++;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 96


100 PATTERN PROGRAMS IN PHP

3. Program to print pascal triangle

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
1 1 1 1
1 2 1 1 2 1
1 3 3 1 1 3 3 1
1 4 6 4 1 1 4 6 4 1
1 5 10 10 5 1 1 5 10 10 5 1

PROGRAM LOGIC

• The logic is same as program B22


• In Pascal's triangle, each number is the sum of the two numbers directly above it.
Read more here
• For first row and first column of each row, initialize the value as 1.
• For the rest calculate by $v = $v * ($row - $column + 1) / $column;

CODE

$row_length = 6;
$v = 1;
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 1; $spaces < $row_length - $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 0; $column <= $row; $column++) {
if ($column == 0 || $row == 0) {
$v = 1;
} else {
$v = $v * ($row - $column + 1) / $column;
}
echo $v . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 97


100 PATTERN PROGRAMS IN PHP

4. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 7 12 16 19 1 +6 +5 +4 +3
2 8 15 23 +1 +6 +7 +8
3 9 14 +1 +6 +5
4 10 +1 +6
5 +1

PROGRAM LOGIC

• It has an inverted right-angle triangle shape; logic is given in program B11


• Calculate the current row number is odd or even.
• Print the current row value in the first column.
• Iterating column wise, take a variable $print_value add 6 and current row value
to it. and print it.
• if row is odd, decrement $print_value by 1, else row is even, increment by 1.

CODE

$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
$counter_value = 6;
$print_value = $row;
echo $print_value."&nbsp;";
for ($column = 1; $column <= $row_length - $row; $column++) {
$print_value += $counter_value;
echo $print_value."&nbsp;";
$counter_value = ($row % 2 == 0) ? $counter_value+1 : $counter_value-1;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 98


100 PATTERN PROGRAMS IN PHP

5. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

5 6 7 8 9 5 +1 +1 +1 +1
4 5 6 7 -1 +1 +1 +1
3 4 5 -1 +1 +1
2 3 -1 +1
1 -1

PROGRAM LOGIC

• It has an inverted right-angle triangle shape; logic is given in program B11


• Observe, as row starts in descending order. First value of row is 5, then 4.
• Iterating column wise, take current row value and decrement by 1.

CODE

$row_length = 5;
$value = 0;
for ($row = 0; $row < $row_length; $row++) {
for ($column = 5 - $row, $value = $column; $column > 0; $column--, $value++) {
echo $value."&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 99


100 PATTERN PROGRAMS IN PHP

6. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 3 5 7 9 1 +2 +2 +2 +2
3 5 7 9 +2 +2 +2 +2
5 7 9 +2 +2 +2
7 9 +2 +2
9 +2

PROGRAM LOGIC

• It has an inverted right-angle triangle shape; logic is given in program B11


• Observe, row starts with 1, iterating row or column wise increment value by 2.

CODE

$row_length = 5;
$start_value = $inner_value = 1;
for ($row = 0; $row < $row_length; $row++, $start_value += 2) {
$inner_value = $start_value;
for ($column = 1; $column <= $row_length-$row; $column++) {
echo $inner_value."&nbsp;";
$inner_value += 2;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 100


100 PATTERN PROGRAMS IN PHP

7. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
3 2 +2 -1
6 5 4 +3 -1 -1
10 9 8 7 +4 -1 -1 -1
15 14 13 12 11 +5 -1 -1 -1 -1

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• As row increases, the first column (marked in green), gets incremented by the
same number as current row value.
• Iterating column wise, (marked in red) decrement value by 1.

CODE

$row_length = 5; $row_counter = 1;
$column_counter = 1; $step_row_value = 1;

for ($row = 1; $row <= $row_length; $row++) {


$column_counter = $row_counter;
for ($column = 1; $column <= $row; $column++) {
echo $column_counter . "&nbsp;";
$column_counter--;
}
$step_row_value++;
$row_counter += $step_row_value;
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 101


100 PATTERN PROGRAMS IN PHP

8. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
2 1 +1 1
3 1 2 +1 1 2
4 1 2 3 +1 1 2 3
5 1 2 3 4 +1 1 2 3 4

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• For the first column of each row (marked in green) print current row value.
• For the second section (marked in red) inside the inner loop, print the current
column value.

CODE

$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
echo $row . "&nbsp;";
for ($column = 1; $column < $row; $column++) {
echo $column . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 102


100 PATTERN PROGRAMS IN PHP

9. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
2 3 2 2 3 2
3 4 5 4 3 3 4 5 4 3
4 5 6 7 6 5 4 4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5 5 6 7 8 9 8 7 9 5

PROGRAM LOGIC

• It is a pyramid shape; logic is given in program B22


• Break the program into two section.
• The left section (marked in green) has spaces and value. Here, number of rows is
equals to number of columns. After printing the spaces use $value_print to print
the output and increment it by 1.
• Adjust the $value_print by decrementing it by 2.
• The right section (marked in red) number of columns is one less than number of
rows. Inside the inner loop print $value_print variable and decrement it by 1.

CODE

$row_length = 5; $value_print = 1;
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;";
}
$value_print = $row;
for ($column1 = 1; $column1 <= $row; $column1++) {
echo $value_print; $value_print++;
}
$value_print -= 2;
for ($column2 = 1; $column2 < $row; $column2++) {
echo $value_print; $value_print--;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 103


100 PATTERN PROGRAMS IN PHP

10. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1 1 2 3 4 4 3 2 1
1 2 3 3 2 1 1 2 3 3 2 1
1 2 2 1 1 2 2 1
1 1 1 1

PROGRAM LOGIC

• It has an inverted right-angle triangle shape; logic is given in program B11


• Break the program into two section.
• Start an outer loop (row) that will iterate 5 times.
• Under the outer loop start an inner loop (column) (marked in green), print the
current column value inside the loop.
• After the first inner loop, start another inner loop (marked in red) that will be
executed same number of times of the other inner loop. To print the pattern,
iterate the loop value descending wise.

CODE

$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
for ($column1 = 1; $column1 <= $row_length-$row; $column1++) {
echo $column1."&nbsp;";
}
for ($column2 = $row_length-$row; $column2 >= 1 ; $column2--) {
echo $column2."&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 104


100 PATTERN PROGRAMS IN PHP

11. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 2 3 4 5 1 2 3 4 5
2 3 4 5 2 3 4 5
3 4 5 3 4 5
4 5 4 5
5 5
4 5 4 5
3 4 5 3 4 5
2 3 4 5 2 3 4 5
1 2 3 4 5 1 2 3 4 5

PROGRAM LOGIC

• The upper section (marked in green) logic is given in the program B15
• The lower section (marked in red) logic is given the program B13

CODE

$row_length = 5;
//upper section
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 0, $value = $row+1; $column < $row_length - $row; $column++,
$value++) {
echo $value."&nbsp;";
}
echo '<br>';
}
// lower section
for ($row = 1; $row < $row_length; $row++) {
for ($spaces = 1; $spaces < $row_length - $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = 0, $value = $row_length - $row; $column <= $row ; $column++,
$value++) {
echo $value."&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 105


100 PATTERN PROGRAMS IN PHP

12. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1 1 2 3 4 4 3 2 1
1 2 3 3 2 1 1 2 3 3 2 1
1 2 2 1 1 2 2 1
1 1 1 1

PROGRAM LOGIC

• The left section (marked in green) logic is given in the program B11
• The right section (marked in red) logic is given the program B15

CODE

$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
//left section starts
for ($column = 1; $column <= $row_length - $row; $column++) {
echo $column . "&nbsp;";
}
for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}

// right section starts


for ($spaces = 0; $spaces < $row; $spaces++) {
echo "&nbsp;&nbsp;&nbsp;";
}
for ($column = $row_length - $row; $column >= 1; $column--) {
echo $column . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 106


100 PATTERN PROGRAMS IN PHP

13. Program to print the full diamond with star pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

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

PROGRAM LOGIC

• The upper section (marked in green) logic is given in the program B22
• The lower section (marked in red) logic is given the program B23

CODE

$row_length = 5;
// upper section starts
for ($row = 1; $row <= $row_length; $row++) {
for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}
// lower section starts
$row_length -= 1;
for ($row = 0; $row < $row_length; $row++) {
for ($spaces = 0; $spaces <= $row; $spaces++) {
echo "&nbsp;";
}
for ($column = 0; $column < $row_length - $row; $column++) {
echo "*&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 107


100 PATTERN PROGRAMS IN PHP

14. Program to print the alphabet pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

A 65
B C 66 67
D E F 68 69 70
G H I J 70 71 72 73
K L M N O 74 75 76 77 78

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• Start with the ASCII value of A (65), then use a counter and increment by 1.

CODE

$row_length = 5;
$ascii_code_start = 65;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
echo chr($ascii_code_start)."&nbsp;";
$ascii_code_start++;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 108


100 PATTERN PROGRAMS IN PHP

15. Program to print the alphabet pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

A 65
B A 66 65
C B A 67 66 65
D C B A 68 67 66 65
E D C B A 69 68 67 66 65

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• Observe, ASCII value increases row wise and decreases column wise by 1

CODE

$row_length = 5;
$ascii_code_start = 65;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = $row; $column >= 1; $column--) {
$print_value = ($ascii_code_start + $column)-1;
echo chr($print_value) . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 109


100 PATTERN PROGRAMS IN PHP

16. Program to print the alphabet pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

Z Y X V V 90 89 88 87 86
Y X V V 89 88 87 86
X W V 88 87 86
W V 87 86
V 86

PROGRAM LOGIC

• It’s an inverted right-angle triangle shape; logic is given in program B11


• Start with the ASCII value of Z (90)
• For first column of each row, set the start value by subtracting ASCII value of Z
by current row value.
• Observe, ASCII value decrement by 1 both row wise and column wise.

CODE

$row_length = 5;
$print_value = $ascii_code_start = 90;
for ($row = 0; $row < $row_length; $row++) {
$print_value = $ascii_code_start - $row;
for ($column = 0; $column < $row_length - $row; $column++) {
echo chr($print_value) . "&nbsp;";
$print_value--;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 110


100 PATTERN PROGRAMS IN PHP

17. Program to print the alphabet pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

A 65
A B 65 66
A B A 65 66 65
A B A B 65 66 65 66
A B A B A 65 66 65 66 65

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• Start with the ASCII value of A (65) and B (66)
• If current column is an odd number print A else B, use this condition (($column %
2 != 0) ? $ascii_code_start : $ascii_code_start +1)

CODE

$row_length = 5;
$ascii_code_start = 65;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
$print_value = ($column % 2 != 0) ? $ascii_code_start : $ascii_code_start +1 ;
echo chr($print_value) . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 111


100 PATTERN PROGRAMS IN PHP

18. Program to print the alphabet pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

E 69
D E 68 69
C D E 67 68 69
B C D E 66 67 68 69
A B C D E 65 66 67 68 69

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• The ASCII value of A is 65 and E is 69. Start from E.
• Observe, ASCII value decreases row wise and increases column wise by 1

CODE

$row_length = 5;
$ascii_code_start = 65;
for ($row = $row_length; $row >= 1; $row--) {
for ($column = $row; $column <= $row_length; $column++) {
$print_value = ($ascii_code_start + $column)-1;
echo chr($print_value) . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 112


100 PATTERN PROGRAMS IN PHP

19. Program to print the half diamond with alphabet pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID


A A
A B A B
A B C A B C
A B C D A B C D
A B C D E A B C D E
A B C D A B C D
A B C A B C
A B A B
A A

PROGRAM LOGIC

• The logic is same as program B26


• Just use character instead of number.

CODE

$row_length = 5;
$ascii_code_start = 65;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
$print_value = ($ascii_code_start + $column) - 1;
echo chr($print_value) . "&nbsp;";
}
echo '<br>';
}
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row_length - $row; $column++) {
$print_value = ($ascii_code_start + $column) - 1;
echo chr($print_value) . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 113


100 PATTERN PROGRAMS IN PHP

20. Program to print the pyramid with alphabet pattern

OUTPUT ON SCREEN UNDERSTANDING THE GRID

A A
B A B B A B
C B A B C C B A B C
D C B A B C D D C B A B C D
E D C B A B C D E E D C B A B C D E

PROGRAM LOGIC

• The logic is same as program B22


• Just use character instead of number.

CODE

$row_length = 5;
$ascii_code_start = 65;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
$print_value = ($ascii_code_start + $column) - 1;
echo chr($print_value) . "&nbsp;";
}
echo '<br>';
}
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row_length - $row; $column++) {
$print_value = ($ascii_code_start + $column) - 1;
echo chr($print_value) . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 114


100 PATTERN PROGRAMS IN PHP

21. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

0 0
1 0 1 +1 -1 1
2 1 0 1 2 +1 -1 -1 1 +1
3 2 1 0 1 2 3 +1 -1 -1 -1 1 +1 +1
4 3 2 1 0 1 2 3 4 +1 -1 -1 -1 -1 1 +1 +1 +1

PROGRAM LOGIC

• Program is divided in two sections. Logic is given in program B9


• For the left section (marked in green) value increases row wise but decreases
column wise by 1.
• For the right section (marked in red) value of first column is fixed at 1, but
increment by 1 when iterated.

CODE

$row_length = 5;
$print_value = 0;
for ($row = 0; $row < $row_length; $row++) {
$print_value += $row;
for ($column = $row; $column >= 0; $column--) {
echo $column . "&nbsp;";
}
for ($column = 1; $column <= $row; $column++) {
echo $column . "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 115


100 PATTERN PROGRAMS IN PHP

22. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

2 2
3 5 3 5
7 11 13 7 11 13
17 19 23 29 17 19 23 29
31 37 41 43 47 31 37 41 43 47

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• Check for prime number, if found print it.
• Inside the inner loop, set the last found prime number in a variable, and break the
loop. So, when you iterate again, it starts from the last found prime number.

CODE

$row_length = 5;
$counter = 0;
$last_found = 1;
for ($row = 1; $row <= $row_length; $row++) {
$counter = 0;
for ($n = $last_found + 1; $n <= 1000; $n++) {
// check for prime number
$flag = 1;
for ($check = 2; $check <= $n / 2; $check++) {
if ($n % $check == 0) {
$flag = 0;
break;
}
}

if ($flag == 1) {
echo $n . "&nbsp;";
$counter++;
if ($row == $counter) {
$last_found = $n;
break;
}
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 116


100 PATTERN PROGRAMS IN PHP

23. Program to print the alphanumeric pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

A1 B2 C3 D4 E5 651 662 673 684 695


B2 C3 D4 E5 662 673 684 695
C3 D4 E5 673 684 695
D4 E5 684 695
E5 695

PROGRAM LOGIC

• The logic is same as program B11


• Observe, starting with ASCII value of A (marked in green) combining with current
row number (marked in red).
• While iterating column wise, character and number both increments by 1.
• Print character and digit separately.

CODE

$row_length = 5;
$ascii_code_start = 65;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 0; $column <= $row_length-$row; $column++) {
$col_val = $row +$column;
$char = ($ascii_code_start + $row + $column) -1;
echo chr($char) . $col_val. "&nbsp;";
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 117


100 PATTERN PROGRAMS IN PHP

24. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
3 2 3 2
4 5 6 4 5 6
10 9 8 7 10 9 8 7
11 12 13 14 15 11 12 13 14 15

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• For odd rows (marked in green), increment the value by 1 and print it,
• For even rows (marked in red), decrement the value by 1 and print it,

CODE

$row_length = 5;
$odd = 1;
$even = 0;
for ($row = 1; $row <= $row_length; $row++) {
$even = $odd + $row - 1;
for ($column = 1; $column <= $row; $column++) {
if ($row % 2 == 0) {
echo $even . "&nbsp;";
} else {
echo $odd . "&nbsp;";
}
$odd++;
$even--;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 118


100 PATTERN PROGRAMS IN PHP

25. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
2 4 +1 +2
7 11 16 +3 +4 +5
22 29 37 46 +6 +7 +8 +9
56 67 79 92 106 +10 +11 +12 +13 +14

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• Initialize a print variable to 1.
• Also, inside the inner loop, increment a counter by 1 and add counter to print
variable

CODE

$row_length = 5;
$step_counter = 0; $print_value = 1;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
echo $print_value . "&nbsp;";
$step_counter++;
$print_value += $step_counter;

}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 119


100 PATTERN PROGRAMS IN PHP

26. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 2 3 4 5 1 2 3 4 5
2 3 4 5 1 2 3 4 5 1
3 4 5 2 1 3 4 5 2 1
4 5 3 2 1 4 5 3 2 1
5 4 3 2 1 5 4 3 2 1

PROGRAM LOGIC

• The upper section (marked in green) logic is given in the program B11
• The lower section (marked in red) logic is given the program B13

CODE

$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column1 = $row; $column1 <= $row_length; $column1++) {
echo $column1;
}
for ($column2 = $row - 1; $column2 >= 1; $column2--) {
echo $column2;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 120


100 PATTERN PROGRAMS IN PHP

27. Program to print the pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
1 0 1 0
1 0 1 1 0 1
1 0 1 0 1 0 1 0
1 0 1 0 1 1 0 1 0 1

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• The trick here is to print the modulus value of the current value of inner column,
use this condition ($column%2)

CODE

$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
echo ($column%2).'&nbsp';
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 121


100 PATTERN PROGRAMS IN PHP

28. Program to print the number pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 1
2 4 2 4
3 5 7 3 5 7
6 8 10 12 6 8 10 12
9 11 13 15 17 9 11 13 15 17

PROGRAM LOGIC

• It is a right-angle triangle shape; logic is given in program B9


• For odd rows (marked in green), increment the value by 2, print only odd number,
• For even rows (marked in red), increment the value by 2, print only even number,

CODE

$row_length = 5;
$odd = 1;
$even = 2;
for ($row = 1; $row <= $row_length; $row++) {
for ($column = 1; $column <= $row; $column++) {
if ($row % 2 == 0) {
echo $even . "&nbsp;";
$even += 2;
} else {
echo $odd . "&nbsp;";
$odd += 2;
}
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 122


100 PATTERN PROGRAMS IN PHP

29. Program to print the number pattern given below

OUTPUT ON SCREEN UNDERSTANDING THE GRID

1 2 3 4 5 1 2 3 4 5
2 1 3 4 5 2 1 3 4 5
3 2 1 4 5 3 2 1 4 5
4 3 2 1 5 4 3 2 1 5
5 4 3 2 1 5 4 3 2 1

PROGRAM LOGIC

• The lower section (marked in green) logic is given in the program B9


• The upper section (marked in red) logic is given the program B15

CODE

$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
for ($column1 = $row; $column1 >= 1; $column1--) {
echo $column1;
}
for ($column2 = $row + 1; $column2 <= $row_length; $column2++) {
echo $column2;
}
echo '<br>';
}

SHAHRUKH KHAN www.thesoftwareguy.in 123


100 PATTERN PROGRAMS IN PHP

Feedback
That’s all for now readers.

This is my first attempt to write a book. I would love to have your feedback either
appreciation or constructive criticism, which will help me to write better next time.

Share your feedback: https://www.thesoftwareguy.in/100-pattern-programs-in-php-


free-ebook

In case have any other query please connect me via email or through social media.

CONNECT WITH ME

EMAIL YOUTUBE FACEBOOK INSTAGRAM

thesoftwareguy7@gmail.com /c/thesoftwareguy7 /thesoftwareguy7 /thesoftwareguy7

Free PHP projects & video tutorials will be available on my YOUTUBE Channel
Make sure you SUBSCRIBE and press the BELL icon, to receive all notifications.

This book comes bundled with source codes and is available for FREE.
All the Content & Rights is reserved to its author.

Remember your only competition is YOU.


Be a better version of what you were YESTERDAY.

SHAHRUKH KHAN www.thesoftwareguy.in 124

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy