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
C Program for Arrow Star Pattern
In C programming, an arrow star pattern is a visual pattern that resembles an arrow shape using asterisk (*) characters. The pattern consists of two parts: an upper section that decreases in width and a lower section that increases in width, creating an arrow-like appearance.
Syntax
void printArrowPattern(int n);
Algorithm
The arrow pattern generation follows these steps ā
- Upper Part: Print decreasing number of stars with increasing leading spaces
- Lower Part: Print increasing number of stars with decreasing leading spaces
- Use nested loops to control spaces and stars for each row
Example
Here's a complete C program to generate an arrow star pattern ā
#include <stdio.h>
void printArrowPattern(int num) {
int i, j;
// Print upper part of the arrow (decreasing stars)
for (i = 1; i <= num; i++) {
// Print leading spaces
for (j = i; j < num; j++) {
printf(" ");
}
// Print stars
for (j = i; j <= num; j++) {
printf("*");
}
printf("<br>");
}
// Print lower part of the arrow (increasing stars)
for (i = 2; i <= num; i++) {
// Print leading spaces
for (j = 1; j < i; j++) {
printf(" ");
}
// Print stars
for (j = 1; j <= i; j++) {
printf("*");
}
printf("<br>");
}
}
int main() {
int num = 4;
printf("Arrow star pattern for n = %d:<br>", num);
printArrowPattern(num);
return 0;
}
Arrow star pattern for n = 4:
****
***
**
*
**
***
****
Example with Different Input
Let's see how the pattern looks with different input values ā
#include <stdio.h>
void printArrowPattern(int num) {
int i, j;
for (i = 1; i <= num; i++) {
for (j = i; j < num; j++) {
printf(" ");
}
for (j = i; j <= num; j++) {
printf("*");
}
printf("<br>");
}
for (i = 2; i <= num; i++) {
for (j = 1; j < i; j++) {
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("*");
}
printf("<br>");
}
}
int main() {
int sizes[] = {3, 5, 6};
int i;
for (i = 0; i < 3; i++) {
printf("Arrow pattern for n = %d:<br>", sizes[i]);
printArrowPattern(sizes[i]);
printf("<br>");
}
return 0;
}
Arrow pattern for n = 3:
***
**
*
**
***
Arrow pattern for n = 5:
*****
****
***
**
*
**
***
****
*****
Arrow pattern for n = 6:
******
*****
****
***
**
*
**
***
****
*****
******
Key Points
- The pattern has 2n-1 total rows for input n
- Upper part has n rows with decreasing stars (n to 1)
- Lower part has n-1 rows with increasing stars (2 to n)
- Time complexity is O(n²) due to nested loops
Conclusion
The arrow star pattern demonstrates effective use of nested loops to create geometric patterns. By controlling spaces and stars systematically, we can generate visually appealing arrow shapes with any desired size.
Advertisements
