Left Arrow Pattern and Right Arrow Pattern in C++
ADVERTISEMENTS
Left arrow pattern and right arrow pattern in c++. In this article, you will learn how to print the left arrow pattern and right arrow pattern in c++.
Source Code
// Left Arrow Pattern and Right Arrow Pattern in C++
#include <iostream>
using namespace std;
int leftRightArrowsPattern(int rows);
// It's the driver function
int main() {
int rows;
cout << "-----Enter the number of rows to print the left & right arrows pattern-----\n";
cin >> rows;
if (rows > 0) {
// calling function to print the pattern
leftRightArrowsPattern(rows);
}
return 0;
}
// This function will print left arrow pattern & right arrow pattern
int leftRightArrowsPattern(int rows) {
int r, r1, c, row1;
row1 = rows;
rows = rows * 4;
cout << "\n----Ths is the left arrow pattern-----\n\n";
for (r = 1; r < rows; r++) {
// to print the left arrow
if (r <= row1) {
for (c = 1; c <= (row1 - r); c++)
cout << " ";
for (c = r;c <= row1; c++)
cout << "*";
cout << "\n";
}
if (r > row1 && r <= row1 * 2) {
for (c = 1; c <= (r - row1); c++)
cout << " ";
for (c = 1; c <= (r - (row1 - 1)); c++) {
if ((r - row1) < row1)
cout << "*";
}
cout << "\n";
}
// to print the right arrow
if (r > row1 * 2 && r <= row1 * 3) {
if (r == (row1 * 2) + 1)
cout << "\n----Ths is the right arrow pattern-----\n\n";
for (c = (r - 1) - (row1 * 2); c >= 1; c--)
cout << " ";
for (c = (3 * row1) - (r - 1); c >= 1; c--)
cout << "*";
cout << "\n";
}
if (r > row1 * 3) {
for (c = (row1 * 4) - (r + 1); c >= 1; c--)
cout << " ";
for (c = (r + 1) - (3 * row1); c >= 1; c--)
cout << "*";
cout << "\n";
}
}
}
Output
-----Enter the number of rows to print the left & right arrows pattern-----
5
----Ths is the left arrow pattern-----
*****
****
***
**
*
**
***
****
*****
----Ths is the right arrow pattern-----
*****
****
***
**
*
**
***
****
*****