Example:
Required knowledge
Basic C programming, LoopLogic to print the given number pattern
Before we get into detail of printing these two patterns I assume that you all must be aware of basic number pattern printing, if not I recommend you to go through some previous number pattern to get yourself acquainted.Now, considering this pattern have an eye to this pattern carefully you will notice two separate patterns here. The two separate patterns are: Now logic to print the both patterns separately is relatively easier then whole pattern at once.
- Run an outer loop from 1 to max-column (where max-column is total number of columns in our case its 5).
- Initialize the inner loop from the current row till max-column.
- Inside inner loop print the current column.
- Run another inner loop after the termination of loop stated in step 2. Initialize it from current row till 1. And print max-column inside this loop.
Program to print the given number pattern
/** * C program to print number pattern */ #include <stdio.h> int main() { int rows, cols, i, j; /* * Reads number of rows, columns to be printed */ printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &cols); for(i=1; i<=rows; i++) { for(j=i; j<=cols; j++) { printf("%d", j); } for(j=i; j>1; j--) { printf("%d", cols); } printf("\n"); } return 0; }
Output
Enter number of rows: 5
Enter number of columns: 5
12345
23455
34555
45555
55555
Enter number of columns: 5
12345
23455
34555
45555
55555
Screenshot
Program to print the given number pattern reverse
/** * C program to print number pattern */ #include <stdio.h> int main() { int rows, cols, i, j; /* * Reads number of rows, columns to be printed */ printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &cols); for(i=rows; i>=1; i--) { for(j=i; j<=cols; j++) { printf("%d", j); } for(j=i; j>1; j--) { printf("%d", cols); } printf("\n"); } return 0; }
Screenshot
Happy coding ;)