Pattern Coding Series
Pattern Coding Series
Pattern
Coding
A comprehensive pdf series with 5 pattern-based
questions in each edition
Edition1 #1/5
ACKNOWLEDGEMENT
As I present the first edition of "Pattern Coding" on this auspicious occasion of World Design Day,
my heart is filled with gratitude and appreciation for those who have contributed to the realization
of this mini-PDF series. This project was initiated with the aim to provide a structured learning
pathway in coding for our juniors and peers at PTU, and its fruition is a testament to the
collaborative spirit of our academic community.
Firstly, I would like to express my profound thanks to Dr.K Sathiyamurthy, Professor CSE and the
Convenor of PTU Design Club, whose guidance has been indispensable. His insights and unwavering
support have not only shaped this edition but have also inspired me to push the boundaries of what
we can achieve through educational initiatives. His dedication to fostering a learning environment
is truly commendable. I thank Dr. G. Ramya, coding trainer,TNP PTU for supporting me with all
necessary content and guidance while in need.
My sincere thanks also go to Yomi, Krishna, Kuhan, and Anupama, whose efforts in organizing and
compiling the content have been vital to this series' success. And my special thanks to Rupam
Chirom, the founder of this club for his constant support and brotherly guidance.
As the former president of the Design Club at PTU, I have witnessed firsthand the vibrant
community we have built together. The intention of this series is to instill the idea that coding more
of pattern-based questions can help us simplify any complex question at ease.
This series would not have been possible without the cumulative effort and enthusiasm of all those
involved. I hope that "Pattern Coding" serves as a valuable resource for many and that it
encourages our students to explore the fascinating world of coding with curiosity and passion.
It is also important to note that while this material begins at a beginner-friendly level, it is designed
to increase in complexity with each subsequent edition, encouraging continual learning and growth.
Lastly, I extend my heartfelt thanks to all the readers and future contributors who will take this
series forward. May this journey of learning and discovery continue to grow and inspire.
2
INDEX
ACKNOWLEDGEMENT…………………………………..2
1.1 PROBLEM 1…………………………………………………..5
1.2 PROBLEM 2………………………………………………….9
1.3 PROBLEM 3………………………………………………….14
1.4 PROBLEM 4………………………………………………....18
1.5 PROBLEM 5………………………………………………….24
DISCLAIMER…………………………………………………28
3
*
**
***
****
*****
4
1) Write program to print pattern of stars, with each row containing an
increasing number of stars up to a specified number of rows set by
the user.
APPROACH:
*
**
The height / number of rows
*** of this X is 5
****
*****
First, let’s get the row height as input from the user.
cin>>rows;
The outer loop iterates through each row of the pattern, starting from
the first row and continuing up to the total number of rows specified by
the user. The loop variable i ranges from 1 to the value of rows,
inclusive.
5
for (int i = 1; i <= rows; i++)
The inner loop prints stars for the current row, with the loop variable j
ranging from 1 to i, inclusive. In each iteration of the inner loop, a star is
printed, and the number of stars printed increases by one with each
iteration of the outer loop.
*
In the second row, ‘i = 2’, it prints two stars and so on.
**
At last, after printing the stars for each row, we use ‘cout<<endl;’ to
move to the next line for the next row.
6
CODE:
#include <iostream>
int main() {
int rows;
cout << endl; // Move to the next line after each row
return 0;
7
*
***
*****
*******
*********
8
2) Write a program to print triangular star pattern
APPROACH:
*
***
***** Height = 5
*******
*********
cin>>n
9
Three integer variables, i, j, k are declared to be used as loop counters in
the subsequent loops.
We start a for loop (outer loop) that will iterate from ‘i = 1’ to ‘i = n’, that
represents each row in the triangular pattern.
Within the outer loop, we use another for loop incrementing from ‘j = 1’
to ‘j = n-i’ , to print the necessary spaces before the stars to left-align
them. We call that as nested J loop.
After printing the spaces, another for loop is initialized to print the
stars. We call it nested K loop.
This loop increments from ‘k = 1’ to ‘k <= 2*i-1’.
In the first row, ‘i = 1’, it prints one star. {4 spaces by J loop and 1 star
by K loop} cout
*
In the second row, ‘i = 2’, it prints three stars{3 spaces by J loop and 3
star by K loop}.
***
At last, after printing the spaces and stars for each row, we use
‘cout<<endl;’ to move to the next line for the next row, creating the
vertical spacing between rows.
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
12
0, 1, 1, 2, 3,
5, 8, 13, 21,
34, 55, 89,
144, 233,
377, 610,
987... 13
3) Noticed the pattern before? Yes, it is indeed the Fibonacci Sequence.
Let’s approach this basic problem systematically
APPROACH:
Fibonacci sequence starts with a 0 and followed by a 1. Then it is the
sum of the two numbers before that follows the sequence.
Let’s explore in detail.
First number: 0 (*default)
Second number: 1 (*default)
Third number: 0+1=1
Now the sequence is 0 1 1
Fourth number is 1+1=2 since it is the addition of previous two
numbers.
Fifth number: 2+1=3
Therefore, the sequence continues to go on as
0 1 1 2 3 5 8 13 21 34 55 89 …
But the core logic is as simple as the sum of the last two numbers.
To keep track of that, we can use temporary variables to store the
numbers.
Let’s get the number of terms to be printed in n
14
cin >> n
Store 0 and 1 in two separate variables t1 and t2 so as to keep track
of the numbers while printing the sequence
int t1 = 0, t2 = 1
Now, create a for loop with i as the iterator that prints the sequence.
If i is 1, print t1 which is 0. If i is 2, print t2.
if(i == 1){
cout<< t1;
continue;
}
The continue keyword is used when a particular condition is met and
you want the for loop to go for the next iteration rather than
executing the code beneath it. It is the opposite of break.
We’ll then have a variable to store the sum of the two numbers as in
this case, it’s t1 + t2 followed by swapping the numbers so that the
previous 2 numbers are replaced with the new ones.
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
15
CODE:
#include <iostream>
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
cin >> n;
if(i == 1) {
continue;
if(i == 2) {
continue;
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
return 0;
16
1
11
121
1331
14641
1 5 10 10 5 1
17
4) The pattern you see is famously known as the Pascal’s Triangle. Let’s
dive deep into the structure
APPROACH:
The code uses a two-dimensional array to store and print Pascal’s
Triangle up to a given number of rows, specified by the user. This
explanation will break down each part of the code for clarity.
18
### Function to Print Pascal's Triangle
void printPascal(int n)
{
int arr[n][n];
- The outer loop (`line`) iterates over each row from `0` to `n-1`.
- The inner loop (`i`) iterates from `0` to the current row number
(`line`). The reason for this is that the number of elements in each row
of Pascal's Triangle is equal to the row number plus one.
19
### Conditionals to Fill the Triangle
if (line == i || i == 0)
arr[line][i] = 1;
else
arr[line][i] = arr[line - 1][i - 1] + arr[line - 1][i];
cout << arr[line][i] << " ";
21
CODE:
#include <bits/stdc++.h>
void printPascal(int n)
int arr[n][n];
if (line == i || i == 0)
arr[line][i] = 1;
else
int main()
int n = 5;
printPascal(n);
return 0;
22
23
Let’s replicate it’s basic structure, shall we?
5) Write a program to print the Cross Sign ( X ) Star Pattern.
APPROACH:
* *
**
The height / number of
* rows of this X is 5
**
* *
First, let’s get the row height as input from the user.
cin>>row_size
Three integers variables, in, out and p are declared to be used as loop
counters in the subsequent loops
The outer for loop is used to control the number of rows in the pattern
1 <= out <= row_size
24
The inner loop is to control the number of characters in each row
1 <= in <= row_size
With this condition, the complete left to right line in the X will be printed
*
*
*
*
*
If you notice the logic behind printing the other stars, we notice that
in+out == row_size+1 i.e. in 1st row, the star needs to be printed at the
5th position. in in this case is 1 and out is 5.
25
So concluding from this, if in + out == row_size +1, then star needs to be
printed which is 5 + 1. This is the case for other rows too. Now
fabricating both the condition into a single if statement will give us
If both the condition fails, print a space “ “ instead. This simple logic
constitutes the X.
26
CODE:
#include<iostream>
int main()
int row_size;
cin>>row_size;
int in,out,p;
for(out=1;out<=row_size;out++)
for(in=1;in<=row_size;in++)
if(in==out || in+out==row_size+1)
cout<<"*";
else
cout<<" ";
cout<<"\n";
27
DISCLAIMER
28