Pattern Problems in C++ Part-1

Simple Patterns Problems using Nested Loops

How to make patterns ?

Firstly take pencil paper and draw a matrix with dots(.) and then joint the points and make the pattern you want on this matrix and find out a specific pattern that diagram follows and make a calculation that you would apply in the loops for forming the pattern. Initially, you find it difficult to make every pattern on paper but this will increase your way of thinking and after practicing these patterns you can solve the loops formula in your mind.

Topics Covered

  1. Print a Solid Rectangle using nested loops
  2. Print a Hollow Rectangle using nested loops
  3. Print a Solid Rhombus using nested loops
  4. Print a Hollow Rhombus using nested loops

Print a solid rectangle using nested loop

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *

CODE:

//program in cpp
#include<iostream>
using namespace std;
int main()
{
	int rows, columns;
	cout<<"Enter the number of rows"<<endl;
	cin>>rows;
	cout<<"Enter the number of columns"<<endl;
	cin>>columns;
	
	//nested loops applied here
	for(int i=1; i<=rows; i++)
	{
		for(int j=1; j<=columns; j++)
		{
			cout<<"*";
		}
		cout<<endl; //change the line
	}
	return 0;
}

OUTPUT :

Print a Hollow rectangle using nested loops

* * * * * *
*         *
*         *
*         *
* * * * * *

CODE:

//program in cpp
#include<iostream>
using namespace std;
int main()
{
	int rows, columns;
	cout<<"Enter the number of rows"<<endl;
	cin>>rows;
	cout<<"Enter the number of columns"<<endl;
	cin>>columns;
	
	//nested loops applied here
    for(int i=1; i<=rows; i++)
    {
    	for(int j=1; j<=columns; j++)
    	{
    		if (i==1 || i==rows || j==1 || j==columns)
    		{
    			cout<<"* ";
			}
			else
			{
				cout<<"  ";
			}
		}
		cout<<endl;
	}
	return 0;
}

OUTPUT:

Print a Solid Rhombus using nested loops

          * * * * * *
        * * * * * *
      * * * * * *
    * * * * * *
  * * * * * *
* * * * * *

CODE:

//program in cpp
#include<iostream>
using namespace std;
int main()
{
	int s;
	cout<<"Enter the value of side "<<endl;
	cin>>s;
	//nested loops starts here
	for(int i=1; i<=s; i++)
	{
		for(int j=1; j<=s-i; j++)
		{
			cout<<"  ";
		}
		for(int j=1; j<=s; j++)
		{
			cout<<"* ";
		}
		cout<<endl;
	}
	return 0;
}

OUTPUT:

Print a Hollow Rhombus using nested loops

          * * * * * *
        *         *
      *         *
    *         *
  *         *
* * * * * *

CODE:

//program in cpp
#include<iostream>
using namespace std;
int main()
{
	int s;
	cout<<"Enter the value of side "<<endl;
	cin>>s;
	//nested loops starts here
	for(int i=1; i<=s; i++)
	{
		for(int j=1; j<=s-i; j++)
		{
			cout<<"  ";
		}
		for(int j=1; j<=s; j++)
		{
			if(i==1 || i==s ||j==1 || j==s)
			{
				cout<<"* ";
			}
			else
			{
				cout<<"  ";
			}
		}
		cout<<endl;
	}
	return 0;
}

OUTPUT:

These are some of the basic and easy pattern problems that we solved above you can copy these codes and try at your own machines and do some experiments in these patterns and comment out the different patterns that you can make from these patterns, modifications like giving space after star(*) in the code (especially in the hollow patterns ) and see the amazing patterns.

In Part-2 of this we will discuss about some of the pyramid patterns using patterns.

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?