In this article, we will learn how to print the left and right arrow patterns using C++.
We will learn how to print the left and right arrows (as shown in the image ) using asterisks. The shape and size of the pattern will depend upon the user input. The input integer must be on an odd number to print the pattern.
We will print the patterns using For loops. There are two functions in the code one for the left arrow and one for the right arrow.
#include using namespace std; void RightArrow(int n) { int m = n/2*3; cout<<"Right Arrow"<<endl; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(j-i==n/2||i==n/2||i+j==m) { cout<<"*"; } else { cout<<" "; } } cout<<"\n"; } } void LeftArrow(int n) { cout<<"Left Arrow"<<endl; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==n/2||i-j==n/2||i+j==n/2) { cout<<"*"; } else { cout<<" "; } } cout<<"\n"; } } int main() { int n; cin>>n; if(n % 2 == 0) { cout<<"n must be odd number"; } else { RightArrow(n); LeftArrow(n); } return 0; }
input - 7
output -
In the above code, i and j are the controlling input of for loops, where i represents the rows and j represents the columns.
Thank you !!
Submitted by Vanshika Vaidya (vaidya27)
Download packets of source code on Coders Packet
Comments