Example:
Input N: 5
Output:
Required knowledge
Basic C programming, LoopLogic to print the given pattern
The given pattern is very much similar to one of previous explained pattern with little trick. To get the logic of this pattern take a moment and think. If still its difficult to get the logic. Read the below observations about the pattern.- It contains total N rows (where N is the total number of rows to be printed).
- Each row contains total i columns (where i is the current row number).
- For each row less than N / 2 current row number i.e. i gets printed.
- For each row greater than N / 2 a value N - i + 1 gets printed.
Program to print the given pattern
/** * C program to print the given number pattern */ #include <stdio.h> int main() { int i, j, N; printf("Enter rows: "); scanf("%d", &N); for(i=1; i<=N; i++) { for(j=1; j<=i; j++) { if(i <= (N/2)) { printf("%d", i); } else { printf("%d", (N - i + 1)); } } printf("\n"); } return 0; }
Output
Enter rows: 5
1
22
333
2222
11111
1
22
333
2222
11111
Screenshot
Happy coding ;)