Geek Culture
Published in

Geek Culture

Decoding the pattern printing problems for beginners — part 1

Ever felt clueless on seeing a pattern printing problem, you are at the right place !

Photo by Martin Sanchez on Unsplash

By the end of this article you will be able to write at least 10 lines of code all by yourself , if you haven’t ever done so. A word of caution, if you’re unsure of the syntax of for loop, learn about it and come back 😊

Now let’s start decoding those pattern problems which are frequently asked in interviews. Open your favorite IDE and code along as you read.

Here are some of the variables that we will be using in our programs.

nst — number of stars.

cst — count of stars.

“The only aim in the life of cst is to catch(note cst starts in c) nst😉.”

Keep on reading this will make sense after some time. It’s important that you use the same variable names mentioned above.

For pattern printing problems we always require at least 2 for loops.

Problem 1 : printing n * n squares

If n = 5 then our output should be :

If n= 2 then our output should be :

Now let’s start coding!!

If n = 5, we will be printing a 5 * 5 squares as shown above. Just look at the first row. Find a relation between n and the stars in the first row. If n = 5, then the first row of our pattern will be having 5 stars. If n = 3, then the first row of our pattern will be having 3 stars. If n = 2, then the first row of our pattern will be having 2 stars.

Thus for any n, we will be having n stars in the first row. If you understand this you have basically solved the problem! We will be initializing nst with this value.

So , Let’s initialize, nst = n

int nst = n; 

We will be using the first for loop for initializing the number of rows in pattern. That is, for n = 5, we will be having 5 rows.

This ,means go from row = 1 to row = n

for(int row =1; row < = n; row++)

Now let’s help our cst to catch nst

for(int cst= 1; cst< = nst; cst++)

Now let’s print star :

printf(“*”);

That’s it!

Complete program :

Problem 2: Right Triangle pattern

Now let’s try another pattern!

For n = 5, we should be printing this pattern :

If n = 5, we will be printing a right triangle as shown above. Just look at the first row. Find a relation between n and the stars in the first row. If n = 5, then the first row of our pattern will be having 1 star. If n = 3, then the first row of our pattern will be having 1 star. Thus, we can find that number of stars in the first row is independent of n. So let’s hardcode the value of nst as 1.

So , Let’s initialize, nst = 1

int nst = 1;

We will be using the first for loop for initializing the number of rows in pattern. That is, for n = 5, we will be having 5 rows.

This ,means go from row = 1 to row = n

for(int row =1; row < = n; row++)

Now let’s help our cst to catch nst !

for(int cst= 1, cst< = nst, cst++)

Now let’s print star :

printf(“*”);

Now we have print a star in the first row. We can observe that the number of stars for each row increases by 1 in our pattern.

So, we will be incrementing the value of nst by a value of 1.

nst = nst + 1

That’s it!

Complete program :

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store