Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program to print right and left arrow patterns in C
In C programming, arrow patterns are visual designs created using nested loops and asterisks. This program demonstrates how to create both left and right arrow patterns using stars and spaces.
Syntax
for(row = 1; row <= n; row++) {
for(col = 1; col <= spaces; col++) {
printf(" ");
}
for(col = 1; col <= stars; col++) {
printf("*");
}
printf("
");
}
Algorithm
- Accept the number of rows for the arrow pattern
- Print the upper part of the arrow (decreasing stars)
- Print the lower part of the arrow (increasing stars)
- Use nested loops to control spaces and stars placement
Example 1: Left Arrow Pattern
The left arrow pattern consists of two parts − an inverted right triangle followed by a right triangle:
#include <stdio.h>
int main() {
int rows = 5;
int r, c;
printf("Left Arrow Pattern:
");
/* Upper part - decreasing stars */
for(r = 1; r < rows; r++) {
for(c = 1; c <= (rows - r); c++) {
printf(" ");
}
for(c = r; c <= rows; c++) {
printf("*");
}
printf("
");
}
/* Lower part - increasing stars */
for(r = 1; r <= rows; r++) {
for(c = 1; c < r; c++) {
printf(" ");
}
for(c = 1; c <= r; c++) {
printf("*");
}
printf("
");
}
return 0;
}
Left Arrow Pattern:
*****
****
***
**
*
*
**
***
****
*****
Example 2: Right Arrow Pattern
The right arrow pattern uses increasing spaces to align stars to the right:
#include <stdio.h>
int main() {
int rows = 5;
int r, c;
printf("Right Arrow Pattern:
");
/* Upper part - decreasing stars with increasing spaces */
for(r = 1; r < rows; r++) {
for(c = 1; c <= (2 * r - 2); c++) {
printf(" ");
}
for(c = r; c <= rows; c++) {
printf("*");
}
printf("
");
}
/* Lower part - increasing stars with decreasing spaces */
for(r = 1; r <= rows; r++) {
for(c = 1; c <= (2 * rows - 2 * r); c++) {
printf(" ");
}
for(c = 1; c <= r; c++) {
printf("*");
}
printf("
");
}
return 0;
}
Right Arrow Pattern:
*****
****
***
**
*
*
**
***
****
*****
Example 3: Combined Arrow Patterns
Here's a complete program that creates both patterns with user input:
#include <stdio.h>
void printLeftArrow(int rows) {
int r, c;
printf("Left Arrow Pattern:
");
for(r = 1; r < rows; r++) {
for(c = 1; c <= (rows - r); c++) {
printf(" ");
}
for(c = r; c <= rows; c++) {
printf("*");
}
printf("
");
}
for(r = 1; r <= rows; r++) {
for(c = 1; c < r; c++) {
printf(" ");
}
for(c = 1; c <= r; c++) {
printf("*");
}
printf("
");
}
}
void printRightArrow(int rows) {
int r, c;
printf("\nRight Arrow Pattern:
");
for(r = 1; r < rows; r++) {
for(c = 1; c <= (2 * r - 2); c++) {
printf(" ");
}
for(c = r; c <= rows; c++) {
printf("*");
}
printf("
");
}
for(r = 1; r <= rows; r++) {
for(c = 1; c <= (2 * rows - 2 * r); c++) {
printf(" ");
}
for(c = 1; c <= r; c++) {
printf("*");
}
printf("
");
}
}
int main() {
int rows = 4;
printLeftArrow(rows);
printRightArrow(rows);
return 0;
}
Left Arrow Pattern:
****
***
**
*
*
**
***
****
Right Arrow Pattern:
****
***
**
*
*
**
***
****
Key Points
- Arrow patterns use nested loops to control spaces and stars
- Left arrow combines inverted and normal triangular patterns
- Right arrow uses calculated spacing to align stars properly
- The number of spaces and stars follows mathematical relationships based on row numbers
Conclusion
Arrow patterns demonstrate the power of nested loops in creating visual designs. By controlling spaces and stars systematically, we can create both left and right pointing arrows with clean, symmetrical appearance.
Advertisements
