Pattern Program in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

A pattern is a design that repeats itself a certain number of times. These can be displayed in C with a firm grasp of the loops, i.e., the for and while loop. A pattern program in C generally contains nested loops where the outer loop controls the number of rows, and the inner loop controls the data in each row containing the contents to be printed.

Introduction

A Pattern is defined as a design that repeats a specific number of times. That pattern can be decorative in real life, like, for example, a rangoli.

A pattern program in C helps improve the concept of looping and algorithms. They are easily displayed with the help of nested loops. Loops can be made using the while or for loop, but writing programs using for loop is more straightforward than the while loop. The outer loop is used for rows, and the inner loop is used to display columns (assume Matrix).

That is why the outer loop controls the number of rows, but the inner loop is used for displaying values in those columns.

Number Pattern

Number patterns usually consist of a sequence of numbers that can be displayed in different structures like a square, half pyramid pattern, pyramid pattern in C etc.

Let us consider a simple pattern to understand the whole mechanism:

Pattern:

On simply observing the pattern, we can see that we would need to use two loops:

  • The first loop iterates over all the rows
  • The second loop iterates over all the columns, or in other words, it is used to control the content in each row.

Let us keep the above 2 points in mind when constructing an approach for this pattern. We can see that the above pattern has the structure of a half pyramid. For such structures, we can use the following two loops:

The idea here is that if we are row number x (0-based indexing), we need to display x+1 elements, or if we are row number x (1-based indexing), we need to display x elements.
This logic can be applied to nearly all patterns using the half pyramid structure.

Now let us move to the contents of each row; each row has a binary output, i.e., either 0 or 1. Perhaps we can use the concept of odd and even numbers for this since they too have a binary output, i.e., the number is either odd or even.

Thus, it is easy to formulate the following logic: If we are at row number i and column number j, then based on the parity of (i+j), we can say:

  • if (i+j) is even, then print 0
  • if (i+j) is odd, then print 1

After this, let us also understand the concept of spaces, which helps us in this pattern and in the ones that follow.

Explanation for the above number pattern

As we can see in the above image, the pattern is a square of size n; it is just we can't see the spaces(which are shown here as hyphens). The logic can be formulated as follows here: if the row contains i elements, then it will contain n-i spaces.

Let us understand this with the help of pattern program in C:

Examples of Number Patterns:

1. Half Pyramid of Numbers

Pattern:

This pattern closely resembles the half pyramid we discussed earlier. The contents of each row can be easily implemented with a single loop.

Pattern Program in C::

2. Inverted Half Pyramid of Numbers

Pattern:

This pattern deals with an inverted half-pyramid. The concept remains the same; just the outer loop runs in the reverse order since the size of each row decreases from n to 1.

Pattern Program in C::

3. Full Pyramid of Numbers

Pattern:

This pattern helps us deal with a new structure, i.e., a full pyramid. There are many ways to formulate a strategy to solve such a pattern however, let us see a relatively simple strategy:

This is the first pattern where we need to use spaces(represented as hyphens in the image below).

Let us divide the pattern into two different patterns:

Explanation for the above pyramid pattern of numbers

Pattern Program in C:

4. Pascal's Triangle

Pattern:

Pascal's Triangle is a triangular array of the binomial coefficients i.e. if we are row i and column, then the pattern will display:

Now let us try to calculate C(i,j) from C(i, j-1):

So C(i, j) can be calculated from C(i, j-1) in O(1)O(1) time. Thus, we can keep a variable whose value can be updated to display the pattern.

Pattern Program in C:

5. Floyd's Triangle

Pattern:

The only trick in this pattern is to keep an extra variable that can be incremented and displayed in each iteration of the loops.

Pattern Program in C:

Star Patterns

Now, let us move to star pattern in C. These very closely resemble the number patterns; the only difference is that we display the asterisk in place of numbers or characters. Let us take a simple example to understand these.

Examples of Star Patterns:

1. Half Pyramid of '*'

Pattern:

All we need to know is the half pyramid structure for displaying such a pattern. Simply two loops while displaying the star should suffice.

Pattern Program in C:

2. Inverted Half Pyramid of '*'

Pattern:

Pattern Program in C:

3. Full Pyramid of '*'

Pattern:

This is precisely the same as we solved with numbers. This is, in fact, much simpler as instead of numbers, only the star needs to be displayed.

Pattern Program in C:

4. inverted Full Pyramid of '*'

Pattern:

If you understood the previous example, then this one should be quite easy.

Pattern Program in C:

Character Patterns

The character patterns may resemble the structure of other patterns we have seen above; however, we display characters rather than stars or numbers.

The critical thing to remember for such patterns is that we can operate the same way with numbers. However, we can typecast the integers to character values using ASCII. Let us see two cases below:

  1. For displaying lower case characters - (a to z)

The ASCII values of lowercase characters range from 97 (a) to 122 (z), thus in order to display them, we can typecast the integer values to character ones using the method below:

Output:

  1. For displaying upper case characters - (A to Z)

The ASCII values of lowercase characters range from 65 (A) to 90 (Z), thus in order to display them, we can typecast the integer values to character ones using the method below:

Output:

Now with this information, let us try to approach the following pattern:

We can observe that this is the half-pyramid structure, where we need to keep an extra variable for displaying the content of the rows similar to the pattern showing Floyd's Triangle. Here, the parity of the row and column sum decides whether the character will be lowercase or uppercase.

Pattern Program in C:

Example of Character Pattern

1. Half Pyramid of Alphabets

Pattern:

This is quite a standard pattern, where 2 loops would suffice.

Pattern Program in C:

Conclusion

  • With a good knowledge of the loops, such as the for and while loops, a pattern program in C is simple to create.
  • We need at least two nested loops to display a pattern program in C.
  • The number of rows is controlled by the outer loop, while the content of each row is controlled by the inner loop.