# How to solve C++ pattern problems

To develop the skills in any Programming language one has to be clear with their basic concept. Last post I wrote about “How you can improve your programming logics“. There are many peoples who are confused with their C++ patterns, almost any kind of patterns can be made in C++. Learn here how to write a program to print patterns. You just have to take a good look at the programming codes needed. All the patterns in C++ can be made using ‘for’ loop. Let’s see how. I am now going to show you step by step to write code for patterns.

## Steps for solving C++ pattern

Step 1: Your first step should be to know what actually the logic you need to write.

Step 2: Write in the paper, what the output you need.

Step 3:
Now start solving the pattern.

Take an example: suppose you have to solve the following simple pattern.

`1111122222333334444455555`

The code should be as follows:

`#include <iostream>using namespace std;int main(){for(int i=1; i<=5; i++){for(int j=1; j<=5; j++){cout<<i;}cout<<endl;}}`

The above code just represents that the “for” loop runs a loop once for every value i.e. the first “for” loop runs the loop 5 times(1-5) and for every value of “i” second “loop” runs the loop 5 times again. The second “for” loop gives the value of “i” which “cout” 1 for five times then 2 for five times then 3 and so on…

So what I mean that you can solve any type of pattern by just “for” loop only. The above code was just very simple, lets know how to write some tough patterns. See the example below.

`122333444455555`
`The program changes a little like  for(int i=1; i<=5; i++){for(int j=1; j<=i; j++){cout<<i;}`

We just changed the second “for” loop so now when the loop runs the first “for” loop runs five times (1-5) but the second “for” loop runs only till “i” times. so the above loop pattern is obtained.

This was a bit tough but there are tougher patterns. Look below:

`    1   22  333 444455555`

Here we need to print the space for first four place and then 1. In second line we need to decrease the space to three place then we have to print “22” then in third line we have to decrease space to two and print “333” and so on…

So the code should contain the the ‘for’ loop for printing space first then another ‘for’ loop for printing numbers so we totally need 3 “for” loops. The code should be as follows:

`#include <iostream>using namespace std;int main(){for(int i=1; i<=5; i++){for(int j=4; j>=i; j--){cout<<" ";}for(int k=1; k<=i; k++){cout<<i;}cout<<endl;}}`

So if you got confused with the above code here is the explanation. The first “for” loop runs a loop of 5. The second “for” loop also runs a loop but in opposite direction i.e. from 4 to ‘i’, where ‘i’ is firstly 1 then 2 and so on. The third “for” loop only prints the last number i.e. 1 then in second line 22 then 33 and so on…The “cout<<i;” is used at last because ‘i’ prints 1, 22, 333, etc., but ‘j’ and ‘k’ can’t print it row-wise.

The patterns in C++ can be more complicated. So hope you understood somethings out of the above post.

## What are the benefits of investing in logistics application development

Gears surrounding the fulcrum act as a force multiplier. Logistics software solutions are the same for the economy of any country. Just about \$10 trillion is a mammoth market for…

## What Are the Key Steps to Develop a Blockchain App?

By offering distributed, safe, open platforms, blockchain technology has transformed sectors. To guarantee that a blockchain app satisfies the intended functionality and security criteria, development of one calls for meticulous…

## The Role of Full Stack Developers in Modern Web Development

Web development is highly lucrative due to its increased demand. The need for a full stack web developer is growing. Thus, this makes it a sought-after skill. The demand for…

## Role of AI in Accelerating Software Development Processes

Artificial Intelligence has gained huge traction in the last few years. However, the growth has significantly risen due to recent technological advancements. AI’s allied disciplines along with machine learning and…

## Step-by-Step Guide: How to Convert EML Files to HTML

In most cases, EML files are used in various email programs for storing single emails or electronic mail. Though most of the email clients support EML format, you canâ€™t (without…

## Game-Changing Benefits of PWA Development for eCommerce Success

The e-commerce industry has been highly competitive since it was launched. The online shopping trend will stay and there will be no decline.  As per recent statistics, the eCommerce market…