# What Are Pattern Problems? | Part 2

What Are Pattern Problems? | Part 2

## Introduction

A repeated series or sequence is known as a pattern. Patterns are everywhere in nature like seasons in nature have a pattern or ripples in water have a pattern. In this article, you are going to master some advanced pattern problems but before that, you must have hands-on practice on solving basic pattern problems discussed here. Here we will learn to print different types of patterns like Pascal Triangle and Floyd’s triangle.

## Pattern 1: Pyramid Pattern Using Alphabets

Here we’ll see the logic to print the pyramid pattern of alphabets.

In this pattern, you have to print one character in the first row, two characters in the second row so one thing is clear that loops will be the same as we use to print triangles of stars.

To print characters in place of stars you just have to do a calculation with ASCII codes of characters here. Have a look at the below code and it’ll be crystal clear to you.

C program to print this Pattern

``````#include<stdio.h>
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",'A'-1 + i);
}
printf("\n");
}
}``````

C++ program to print this pattern

``````#include <iostream>
using namespace std;

int main()
{
char input, alphabet = 'A';

cout << "Enter the uppercase character you want to print in the last row: ";
cin >> input;

for(int i = 1; i <= (input-'A'+1); ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << alphabet << " ";
}
++alphabet;

cout << endl;
}
return 0;
}``````

Java program to print this pattern

``````public class RepeatingPattern {
public static void main(String[] args) {

//ASCII value of capital A is 65
int alpha = 65;

//loop for rows
for (int i = 0; i <= 9; i++) {
//loop for columns
for (int j = 0; j <= i; j++) {
//prints the character
System.out.print((char) letter + " ");
}
alpha++;
System.out.println();
}
}
}``````

## Pattern 2: Diamond Pattern Using Stars

Everyone loves Diamond. I know you were thinking about how to print a pattern in a diamond shape.

So here we’ll see the code to print a diamond-shaped pattern:

This might look a bit complex to you but once you observe the pattern hidden behind it then it’ll be super easy to code. The upper part of this pattern is the same as printing a pyramid pattern with n rows and the lower half of this pattern is the same as printing an inverted pyramid pattern with (n-1) rows. Now try coding this pattern here. First, try to print its upper half and then it’s the lower half.

C program to print this pattern

``````#include <stdio.h>

int main() {
int n, s, r;

printf("Enter number of rows\n");
scanf("%d", & n);

//printing upper half
for (r = 1; r <= n; r++) {
//printing spaces in columns
for (s = 1; s <= n - k; s++)
printf(" ");
//printing star in columns
for (s = 1; s <= 2 * k - 1; s++)
printf("*");

printf("\n");
}

//printing lower half
for (k = 1; k <= n - 1; k++) {
for (c = 1; c <= k; c++)
printf(" ");

for (c = 1; c <= 2 * (n - k) - 1; c++)
printf("*");

printf("\n");
}

return 0;
}``````

C++ program to print this pattern

``````#include<iostream>
using namespace std;
int main()
{
int n, c, k, space = 1;
cout<<"\n Enter Number of Rows : ";
cin>>n;
space = n - 1;
for(k = 1; k<=n; k++)
{
for (c = 1; c<=space; c++)
cout<<" ";
space--;
for (c = 1; c<= 2*k-1; c++)
cout<<"*";
cout<<"\n";
}
space = 1;
for(k = 1; k<= n - 1; k++)
{
for (c = 1; c<= space; c++)
cout<<" ";
space++;
for (c = 1 ; c<= 2*(n-k)-1; c++)
cout<<"*";
cout<<"\n";
}
return 0;
}``````

Java program to print this pattern

``````import java.util.Scanner;
public class pattern {
public static void main(String args[]) {
int row, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner sc = new Scanner(System.in);
row = sc.nextInt();
space = row - 1;
for (j = 1; j <= row; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= row - 1; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space++;
for (i = 1; i <= 2 * (row - j) - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
}
}``````

## Pattern 3: Pascal’s Triangle Pattern

I’m sure you all have used Pascal’s triangle in mathematics. Now is the time to code it, but before coding it, let’s see what pattern we need to observe in it:

Now see,
Row 0 = 1
Row 1 = (0+1) , (1+0) = 1 1
Row 2 = (0+1), (1+1), (1+0) = 1 2 1
Row 3 = (0+1), (1+2), (2+1), (1+0) = 1 3 3 1

Now you must have got the pattern, i.e., all the edges will be filled with 1, and other numbers in the row will be the sum of two numbers present in the previous row. Try coding it here.

C program to print Pascal’s Triangle

``````#include <iostream>
using namespace std;

int main()
{
int rows, coeff = 1;

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

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

for(int j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
coeff = 1;
else
coeff = coeff*(i-j+1)/j;

cout << coeff<< "   ";
}
cout << endl;
}

return 0;
}``````

## Pattern 4: Floyd’s Triangle Pattern

Floyd’s triangle pattern contains consecutive natural numbers. Let’s see how it looks:

If you have understood all the other patterns discussed above, then I’m sure it will be super easy for you to code this pattern. Before seeing the solution below, I recommend trying it on your own once.

C program to print Floyd’s Triangle

``````#include <stdio.h>
int main() {

int rows, i, j, num = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);

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

C++ program to print Floyd’s Triangle

``````#include <iostream>
using namespace std;

int main()
{
int rows, num= 1;

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

for(int i = 1; i <= rows; i++)
{
for(int j = 1; j <= i; ++j)
{
cout << num << " ";
++num;
}

cout << endl;
}

return 0;
}``````

## Frequently Asked Questions

What are structural design patterns?

Structural patterns are concerned with arranging objects and classes so that one can form flexible and efficient large structures.

How to master Pattern Problems?

You should be thorough with loops working if you want to master patterns. Observation and practice are the best ways to master pattern problems.

How to analyse the Patterns?

To analyse patterns observation is the key thing. Suppose you are given a complex pattern to print, then you should try to break it into smaller patterns. Like in diamond pattern problems you can make use of concepts learned in a printing pyramid pattern.

Which loop should I use for printing a pattern?

Patterns can be printed using for loop or while loop or do-while loop. The choice of the loop in your code will depend upon the type of pattern, you want to create. Sometimes you can print a pattern using both for loop as well as while loop. With practice, you can become quick at deciding the type of loop you should use for a particular pattern.

## Key Takeaways

Solving Pattern problems require observation. You can solve complex pattern problems easily by just breaking them into smaller subproblems. Try to ace smaller patterns first and then connect them all to get a solution to a complex pattern problem. Solving pattern problems is the best way to master loops in programming.

In this article, you mastered different types of pattern problems like patterns containing alphabets, pascal triangle and Floyd’s triangle etc. Since you have learned to solve different kinds of pattern problems here so now is the time for practice and here is an assignment for you, try coding this pattern problem here

By Deeksha Sharma