Coders Packet

All Basic Pattern Solving Problems using FOR Loop in C++

By Rahul Chandrakant Suthar

In this project, I will show you how to print following pattern (Example Images) using FOR loop in C++ programming language.

With the help of examples, we will first look at how to utilize C++ programming to interact with various star pattern programs in this article.

In the C++ programming language, you just need to utilize two or three loops to generate patterns. How many loops you use will depend on the pattern you need to design. One for a row and one for a column are utilized as the minimal number for a pattern. The first loop is referred to as an outer loop and displays the rows, while the second loop is referred to as an inner loop and displays the columns.

To make the idea of patterns in C++ simpler to understand, let's talk about a few examples.

Example 1:

#include
using namespace std;
 
int main(){
    int row,col;
    //rectangle pattern printing
    cout<<"Rectangle pattern"<<endl;
    cout<<"enter row"<<endl;
    cin>>row; //input row
    cout<<"enter col"<<endl;
    cin>>col; //input column
 
    for (int i = 1; i <= row; i++) //loop for row
    {
        for (int j = 1; j <= col; j++) //loop for column
        {
            cout<<"*";
        }
        cout<<endl;
    }
   
    return 0;
}

Example 2:

#include
using namespace std;

int main(){
    int row,col;
    //hollow rectangle pattern printing
    cout<<"Rectangle pattern"<<endl;
    cout<<"enter row"<<endl;
    cin>>row;//input row
    cout<<"enter col"<<endl;
    cin>>col;//input column

    for (int i = 1; i <= row; i++) //loop for row
    {
        for (int j = 1; j <= col; j++) //loop for column
        {
           if (i == 1 || i == row || j == 1 || j == col) //condition for printing star
           {
                cout<<"*";
           }
           else{
                cout<<" ";
           }
           
        }
        cout<<endl;
    }
    
    return 0;
}

Example 3:

 

#include
using namespace std;

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

Example 4:

#include
using namespace std;

int main(){
    int n;
    cin>>n;
    for(int i = 1; i<=n ; i++){
        for(int j = 1; j<= i;j++){
            cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

Example 5:

#include
using namespace std;

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(j<=n-i)
            {
                cout<<" ";
            }
            else{
                cout<<"*";
            }
            
        }
        cout<<endl;
    }
    return 0;
}

Example 6:

#include
using namespace std;

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            cout<<i;
        }
        cout<<endl;
    }
    return 0;
}

Example 7:

#include
using namespace std;

int main(){
    int n;
    cin>>n;
    int count = 1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            cout<<count<<" ";
            count++;
        }
        cout<<endl;
    }
    return 0;
}

Example 8:

#include
using namespace std;

int main(){
    int n;
    cout<<"N = ";
    cin>>n; //taking input
    cout<<"\n";
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            cout<<"*";
        }
        int space = 2*n - 2*i; // space formula for pattern
        for (int j = 1; j <=space; j++)
        {
            cout<<" ";            /* code */
        }
        for(int j=1;j<=i;j++){
            cout<<"*";
        }
        cout<<endl;
    }

    for(int i=n;i>=1;i--){
        for(int j=1;j<=i;j++){
            cout<<"*";
        }
        int space = 2*n - 2*i;
        for (int j = 1; j <=space; j++)
        {
            cout<<" ";            /* code */
        }
        for(int j=1;j<=i;j++){
            cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Rahul Chandrakant Suthar (Rahulcs27)

Download packets of source code on Coders Packet