Pattern Question ( hollow rectangle pattern in c++ ) with source code and small explanation 02 || Print pattern using c++ with 5 rows and 4 columns

 Here is the pattern that I am going to print using  c plus c plus , you can check algorithm and code as well in below of the image.








  1. include header file iostream
  2. using namespace std : which show we took thing under standard or std
  3. main() function - starting poin
  4. declared variable along with datatype
  5. take input using cin , input is a
  6. used endl for line break
  7. again take input using cin , input is b
  8. use for loop for rows and it goes to till a
  9. again use for loop for column it goes to till b
  10. apply if statement that  a is total rows in the pattern and b is total column in the pattern so at the first and last position of the row and column we print starts (*)  else print space.
  11. i == 1 checks first row , i == a checks last row and j == 1 checks first column and j == b checks last column where we have to print stars
  12. after the column loop  or inner loop which goes till 'j' , we add line break so other rows can be print.
  13. that's all !

You can copy this code as well !

#include <iostream>
using namespace std;

int main(){
    int a , b;
    cin>>a;
    cout<<endl;
    cin>>b;

    for (int i = 1; i <= a; i++)
    {
        for (int j = 1; j <= b; j++)
        {
            if(i == 1  || i == a || j == 1 || j == b){
                cout<<"*";
            }else{
                cout<<" ";
            }      
        }
        cout<<endl;
    }
       
}

Comments