Right Arrow Pattern Program In Python
Print an arrow pattern using hyphens and asterisks based on an odd integer N
Understand the Problem
Problem Statement
The program must accept an odd integer N as the input. The program must print N lines containing hyphens and asterisks based on the following conditions:
- In the line 1, the program must print (N-1)/2 hyphens followed by an asterisk.
- In the line 2, the program must print (N-1)/2 + 1 hyphens followed by an asterisk.
- In the line 3, the program must print (N-1)/2 + 2 hyphens followed by an asterisk.
- Similarly, the program must print the first (N-1)/2 lines as the output.
- In the line (N+1)/2, the program must print N asterisks.
- In the line (N+1)/2 + 1, the program must print N-2 hyphens followed by an asterisk.
- In the line (N+1)/2 + 2, the program must print N-3 hyphens followed by an asterisk.
- In the line (N+1)/2 + 3, the program must print N-4 hyphens followed by an asterisk.
- Similarly, the program must print the remaining lines as the output.
Constraints
- 5 <= N <= 99
- N must be an odd integer
- Each line consists only of hyphens (-) and asterisks (*)
- The pattern forms a right-facing arrow shape
Examples
5--*
---*
*****
---*
--*For N=5, the pattern has: - Line 1: (5-1)/2 = 2 hyphens + 1 asterisk = '--*' - Line 2: 2+1 = 3 hyphens + 1 asterisk = '---*' - Line 3: 5 asterisks = '*****' - Line 4: 5-2 = 3 hyphens + 1 asterisk = '---*' - Line 5: 5-3 = 2 hyphens + 1 asterisk = '--*'
7---*
----*
-----*
*******
-----*
----*
---*For N=7, the pattern has: - Lines 1-3: Increasing hyphens (3, 4, 5) followed by asterisk - Line 4: 7 asterisks forming the arrow's center - Lines 5-7: Decreasing hyphens (5, 4, 3) followed by asterisk
Solution
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
int mid = (N + 1) / 2;
int spaces;
// Upper half of the arrow
for (int i = 1; i < mid; i++) {
spaces = (N - 1) / 2 + (i - 1);
for (int j = 0; j < spaces; j++) {
printf("-");
}
printf("*");
printf("\n");
}
// Center line (full width)
for (int i = 0; i < N; i++) {
printf("*");
}
printf("\n");
// Lower half of the arrow
for (int i = mid + 1; i <= N; i++) {
spaces = N - (i - mid) - 1;
for (int j = 0; j < spaces; j++) {
printf("-");
}
printf("*");
printf("\n");
}
return 0;
}Step 1: Read the odd integer N from standard input.
Step 2: Calculate the middle line index as (N+1)/2.
Step 3: Generate the upper half by iterating from 1 to mid-1. For each line i, print (N-1)/2 + (i-1) hyphens followed by an asterisk.
Step 4: Print the center line with N asterisks.
Step 5: Generate the lower half by iterating from mid+1 to N. For each line i, print N-(i-mid)-1 hyphens followed by an asterisk.
Step 6: Each line ends with a newline character.