What Are Pattern Problems? Part – 1

What Are Pattern Problems? Part - 1
What Are Pattern Problems? Part - 1

Introduction

Patterns are there in almost everything around us. Seasons in nature have patterns, tides in water have a pattern, day and night in nature have a pattern. So it’s necessary to understand patterns. If you want to ace your interviews then you should be very clear about the logic used in printing patterns.

In this article, you will learn to print different types of patterns like star patterns and pyramid patterns but before that let’s see how you can define a pattern.

What is a Pattern? 

A repeated series or sequence is known as a pattern. To solve a problem related to a pattern, you just have to figure out its fashion of repeating through your observation. Math patterns follow some rules in maths, and patterns in nature follow a specific manner of repeating themselves.

You just need to observe carefully to decode any pattern, whether that’s a pattern problem in your interview or you have to make predictions about something in nature. Here we will be learning how to solve pattern problems in interviews.

Let’s get started 🙂

Pattern 1: Star Patterns

These are the most common type of pattern problems asked in interviews. These star patterns can be of any shape like a rectangle, pyramid, and so on.

Let’s master them here one by one:

Rectangle Pattern 

We’ll learn to print these rectangle patterns in all four programming languages, i.e., C, C++, Python, and Java. So, let’s jump into it.

Solid Rectangle

No of rows: 3
No of columns: 5
To print this pattern, you need a loop to iterate through all the rows and a loop to iterate through all the columns.

Let’s see a general overview to solve a pattern problem.

C program to print this pattern

#include <stdio.h>

void printRectangle(int n, int m) {
  int i, j;

//iterating through rows
  for (i = 1; i <= n; i++) {
//iterating through columns
    for (j = 1; j <= m; j++) {
      printf("*");
    }

    printf("\n");
  }
}

int main() {

  int rows, columns;

  printf("\nEnter the number of rows :");
  scanf("%d", & rows);

  printf("\nEnter the number of columns :");
  scanf("%d", & columns);
  printf("\n");

  printRectangle(rows, columns);
  return 0;
}

So from the above code, it’s clear that it’s super easy to code for a pattern problem if you have understood the general overview. Let’s see the code in other programming languages also:

C++ program to print this pattern

#include <iostream>

using namespace std;

void printRectangle(int n, int m) {
  int i, j;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cout << "*" << " ";
    }
    cout << endl;
  }
}

int main() {
  int rows, columns;
  cout << "Enter the number of rows :" << endl;
  cin >> rows;

  cout << "Enter the number of columns : " << endl;
  cin >> columns;

  printRectangle(rows, columns);
  return 0;
}

Java program to print this pattern

import java.util.*;
public class Main {
    static void printRectangle(int n, int m) {
        int i, j;
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }

    }

    public static void main(String args[]) {
        int rows, columns;
        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter the number of rows : ");
        rows = sc.nextInt();
        System.out.print("\nEnter the number of columns : ");
        columns = sc.nextInt();
        System.out.print("\n");
        printRectangle(rows, columns);
    }

}

Python program to print this pattern

def printRectangle(row, col) :
	for i in range(0, row) :
		for j in range(0, col) :
			 print('*', end = '  ')
			
		print()	
				

row = 3
col = 5
printRectangle(row, col)

So we’ve seen the way to print rectangular patterns. Now let’s move to pyramid-shaped patterns.

Pattern 2: Pyramid Patterns

Pyramid patterns can be of various shapes like an inverted pyramid, hollow pyramid. Let’s master them here one by one: 

Full Pyramid

In this pattern for row 1, we need to print 1 star and six spaces. For row 2, we need to print five spaces and two stars. Try to code this problem here on your own before moving on to the below solution.

C Program to print this pattern  

#include <stdio.h>  
#include <conio.h>  
void main()  
{  
    int i, j, rows, k = 0;  
    printf (" Enter the number of rows: \n");  
    scanf ("%d", &rows);   
     for ( i =1; i <= rows; i++)  
    {  
        for ( j = 1; j <= rows - i; j++)  
        {  
            printf ("  ");   
        }  
        for ( k = 1; k <= ( 2 * i - 1); k++)  
        {  
            printf ("* "); 
        }  
        printf ("\n");  
    } 
 }  

C++ program to print this pattern

#include <iostream>
using namespace std;

int main()
{
    int s, rows;

    cout <<"Enter number of rows: ";
    cin >> rows;

    for(int i = 1, k = 0; i <= rows; ++i, k = 0)
    {
        for(s = 1; s <= rows-i; ++s)
        {
            cout <<"  ";
        }

        while(k != 2*i-1)
        {
            cout << "* ";
            ++k;
        }
        cout << endl;
    }    
    return 0;
}

Java program to print this Pattern    

public class Pattern {
    public static void main(String args[]) {
        int i, j, row = 6;
        //loop for rows 
        for (i = 0; i < row; i++) {
            //loop to print spaces     
            for (j = row - i; j > 1; j--) {
                System.out.print(" ");
            }
            //loop for columns  
            for (j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
} 

Pattern 3: Pyramid Patterns Using Numbers

We have learned how to print pyramid patterns of stars, but sometimes we have to print pyramids containing numbers. So let’s see how to deal with number pyramids:

Inverted Half Pyramid: 

In this pattern, the total number of rows is 5. Now observe the logic carefully. For row 1, we have to print numbers up to 5, and then in row 2, we have to print numbers up to (Total Number of rows-1). You will get it more clearly by seeing the below code.

C program to print this Pattern 

#include <stdio.h>
int main() {
   int i, j, rows;
   printf("Enter the number of rows: ");
   scanf("%d", &rows);

   for (i = rows; i >= 1; --i) {
      for (j = 1; j <= i; ++j) {
         printf("%d ", j);
      }
      printf("\n");
   }
   return 0;
}

C++ program to print this pattern

#include <iostream>
using namespace std;

int main()
{
    int rows;

    cout << "Enter number of rows: ";
    cin >> rows;

    for(int i = rows; i >= 1; --i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << j << " ";
        }
        cout << endl;
    }
    return 0;
}

Frequently Asked Questions

What are examples of Patterns?

A series that repeats itself is known as a pattern. Examples of patterns include 1 3 5 7 9…… Observe the pattern here, every element is obtained by adding 2 in the previous element.

Let’s see a visual pattern example:

By seeing this pattern, you can quickly tell what is going to replace this question mark.

What is the formula for patterns?

You can form a formula by seeing a pattern and can predict the next outcome of that pattern easily by using a formula. Suppose you have a pattern as 1 2 4 8 10 …… .So the first number in this pattern is 2^0, the second number is 2^1. Hence, the nth term of this pattern will be 2^(place of that term-1).

What are the five patterns in nature?

Five patterns in nature are as follows:

What is a geometric pattern?

A pattern that is made by involving geometric shapes only is known as a geometric pattern.

How many loops do I have to use in a program to code a pattern?

The number of loops in your code will depend upon the type of pattern, but if you create a pattern, at least two loops should be there, one for row and one loop for the column.

Key Takeaways

Solving pattern problems improves your logic of using loops in computer science. You can use it for loop or while loop depending upon the type of pattern you want to create. In this article, you mastered the different types of pattern problems like rectangle pattern, pyramid star pattern, etc.

Now here is an assignment for you, after reading this article, try coding this pattern problem here. Remember, observation and practice are the ways by which you can code any pattern problem easily in your interview.

By Deeksha Sharma