Coders Packet

Different Types Of Star Patterns In C++

By Ashwini Laxman Jadhav

Hello guys, In this tutorial, we will learn how to print different types of star patterns in c++ language. With more easiest and simplest way.

In this tutorial, I am going to give you the simplest hint to print any kind of pattern in c++ language.

Printing Different Star Patterns In C++.

For printing patterns, we are using for loops concept here, as we need to print different patterns we need to take care of the following some basic things:-

1] analyze your pattern and try to figure out how many stars are needed in a single row.

2] analyze how many columns you need.

3] analyze if is there any space in between your pattern if yes then you need three loops one for row another for space and lastly for col.

Some basic points:- your answer to how to print this pattern or that pattern is itself in your pattern only, sometimes the pattern is more confusing but mostly all patterns depend upon the number of rows because your outer loop is more reasonable for your pattern.

 

For example:- You have one pattern like

****

*    *     

*    *

****

So, here might think about how to do this pattern it is so easy just analyze a few steps,

a) rows have stars on their first stage or else they are in the last stage.

b) col has a star on their first stage or else their last stage.

Take two for loops, one for rows and another for columns and take the above condition in between them.

and your pattern is ready to print.

One thing which is usually we are not worried about but in star patterns or any patterns must take care of spaces or new lines this both plays vital role in printing output.

Here is some code if you want all patterns then download the whole source code from the below file section.

#include
using namespace std;

//Hrer is the pattern like
/*

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

*/

int main()
{
  int n,m;
  cin>>n>>m;
  
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=n;j++)
    {
      cout<<"*";
    }
    cout<<"\n";
  }
}

 

 

 

 

Download Complete Code

Comments

No comments yet