Program to print number pattern
55555
54444
54333
54322
54321
C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)
9529Program:
/** * C program to print number pattern * www.atnyla.com */ #include int main() { int rows, cols, i, j; /* Input rows and columns from user */ printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &cols); for(i=1; i<=rows; i++) { for(j=cols; j>cols-i; j--) { printf("%d", j); } for(j=1; j<=cols-i; j++) { printf("%d", (rows - i + 1)); } printf("\n"); } return 0; }
Output:
Enter number of rows: 5 Enter number of columns: 5 55555 54444 54333 54322 54321
Explanation:
Required knowledge
Basic C programming, Loop
Logic to print the given patterns
Before all of these patterns confuse you, I recommend you to learn some basics of number pattern logic.
Once you get acquainted with basics of number pattern printing logic. If you look to these pattern carefully you will find that all of them are similar. Hence if you can decode the logic of any rest of them will be easy to code. Now, have take a minute and look to the below pattern carefully.
55555 54444 54333 54322 54321
If you have looked carefully you can actually divide this pattern in two parts, let me show you.
5---- 54--- 543-- 5432- 54321
-5555 --444 ---33 ----2 -----
Now, I hope that logic of whole pattern would have been cleared to you somewhat. If not let me explain the logic to print this pattern.
- To iterate through rows, run an outer loop from 1 to rows (where rows is total rows to be printed).
- To print first part of pattern, run an inner loop from columns to columns - current_row (where columns is total columns to be printed). Inside this loop print the value of current column.
- To print the second part of pattern, run another inner loop from 1 to columns - current_row. Inside this loop print the value of rows - current_row + 1.
Lets implement this...
Note: Logic to all remaining patterns are similar so I won't be explaining logic of remaining three patterns. But source code of all t
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.