Printing Different pattern Bash in C++


This article is intended to print a half-pyramid pattern bash using the C++ programming language. In the view of the stipulated pattern to be printed, the following algorithm is being orchestrated to achieve our goal as;

Algorithm

Step-1 Set the length of the Bash (Height)
Step-2 Outer loop to handle the number of rows
Step-3 Inner loop to handle columns
Step-4 Print the pattern with the character (@)
Step-5 Set the pointer to a new line after each row (outside the inner loop)
Step-6 Repeat the loop till the Bash Height

Example

So, the following C++ source code is ultimately carved out by complying the aforesaid algorithm as follows;

 Live Demo

#include <iostream>
using namespace std;
void PrintBash(int n){
   // outer loop to handle number of rows
   for (int i=0; i<n; i++){
      // inner loop to handle number of columns
      for(int j=0; j<=i; j++ ){
         // printing character
         cout << "@ ";
      }
      // ending line after each row
      cout << endl;
   }
}
int main(){
   int Len = 6;
   PrintBash(Len);
   return 0;
}

Output

After compilation of the above code, the half-pyramid will be printed looks like as.

@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
@ @ @ @ @ @

Updated on: 16-Jan-2020

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements