Example:
Input N: 5
Output:
Required knowledge
Basic C programming, LoopLogic to print the given number pattern
To get the logic of the pattern have a close look to the pattern for a minute. You might notice following things about the pattern.- It contains N rows (where N is the total number of rows to be printed).
- Each row contains exactly i columns (where i is the current row number).
- For all odd rows it contains numbers in increasing order from 1 to i. Hence the inner loop formation to print the odd rows will be for(j=1; j<=i; j++).
- For all even rows it contains numbers in decreasing order from i to 1. The inner loop formation to print the even rows will be for(j=i; j>=1; j--).
Program to print the given number 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++) { if(i & 1) { // Prints numbers for odd row for(j=1; j<=i; j++) { printf("%d", j); } } else { // Prints numbers for even row for(j=i; j>=1; j--) { printf("%d", j); } } printf("\n"); } return 0; }
Output
Enter rows: 5
1
21
123
4321
12345
1
21
123
4321
12345
Screenshot
Happy coding ;)