easy
1 views
Pattern Printing – Till N & Reverse
Print a symmetric pattern of numbers increasing from 1 to N and then decreasing back to 1
Understand the Problem
Problem Statement
Problem Statement
Write a program that accepts an integer N and prints a total of 2N lines, following the pattern shown in the examples below.
The pattern consists of:
- Lines 1 to N: Each line i contains the digit i repeated i times
- Lines N+1 to 2N: Each line contains the digit (2N - line_number) repeated the appropriate number of times
Constraints
- 2 ≤ N ≤ 100
- N must be a positive integer
- Output must contain exactly 2N lines
Examples
Example 1
Input
4Output
1
22
333
4444
4444
333
22
1Explanation
For N=4, we print 8 lines total. First 4 lines show digits 1, 22, 333, 4444. Next 4 lines show the same pattern in reverse: 4444, 333, 22, 1.
Example 2
Input
7Output
1
22
333
4444
55555
666666
7777777
7777777
666666
55555
4444
333
22
1Explanation
For N=7, we print 14 lines total. First 7 lines show digits 1, 22, 333, 4444, 55555, 666666, 7777777. Next 7 lines show the same pattern in reverse order.
Solution
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
// Print ascending pattern
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
// Print descending pattern
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
return 0;
}Time:O(N²) - We iterate through N lines, and for each line i, we print i characters
Space:O(1) - We only use a constant amount of extra space for variables
Approach:
The C solution uses nested loops:
- First loop (i = 1 to N): For each line i, prints the digit i repeated i times using an inner loop
- Second loop (i = N down to 1): Prints the same pattern in reverse order
Each line ends with a newline character to move to the next line.
Visual Explanation
Loading diagram...