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.
Input Format
The first line contains the integer N
.
Output Format
Print the pattern in 2N
lines as described.
Boundary Conditions
2 ≤ N ≤ 100
Example Input/Output
Example 1
Input:
4
Output:
1 22 333 4444 4444 333 22 1
Example 2
Input:
7
Output:
1 22 333 4444 55555 666666 7777777 7777777 666666 55555 4444 333 22 1
Python Implementation
n=int(input());p=[]
for i in range(1,n+1):
l=[]
for j in range(i):l.append(i)
for i in l:print(i,end='')
print();p.append(l)
for i in p[::-1]:
for j in i:print(j,end='')
print()
C Implementation
#include
#include
int main() {
int n, a = 1, i, j;
scanf("%d", &n);
// Printing the upper pattern
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++)
printf("%d", a);
a++;
printf("\n");
}
// Printing the reverse pattern
for (i = 0; i < n; i++) {
a--;
for (j = 0; j < n - i; j++)
printf("%d", a);
printf("\n");
}
return 0;
}
Why Practice Pattern Printing?
Pattern printing exercises are crucial for understanding nested loops and logic-building in programming. Whether you use Python or C, practicing such problems will enhance your coding skills and logical thinking.
Conclusion
We hope this article helps you master the art of pattern printing. Try implementing the code examples provided, experiment with different values of N
, and explore variations of the pattern. Happy coding!
Leave a Reply