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.
- include header file iostream
- using namespace std : which show we took thing under standard or std
- main() function - starting poin
- declared variable along with datatype
- take input using cin , input is a
- used endl for line break
- again take input using cin , input is b
- use for loop for rows and it goes to till a
- again use for loop for column it goes to till b
- 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.
- 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
- after the column loop or inner loop which goes till 'j' , we add line break so other rows can be print.
- 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
Post a Comment