Program to print the given number pattern
55555
4444
333
22
1
C Programming Language / Loop control in C Language
26620Program:
/** * C program to print number pattern * www.atnyla.com */ #include int main() { int i, j, N; printf("Enter N: "); scanf("%d", &N); for(i=N; i>=1; i--) { for(j=1; j<=i; j++) { printf("%d", i); } printf("\n"); } return 0; }
Output:
Enter N: 5 55555 4444 333 22 1
Explanation:
Required knowledge
Basic C programming, Loop
Logic to print the given number pattern 1
Before we discuss about logic of printing these patterns. I would recommend you to check out some similar number patterns. As both of the patterns mentioned in this program are just vertically flipped image of
1 22 333 4444 55555
Now, moving on to the logic of first pattern that we need to print. Have a careful eye on to the below pattern
55555 4444 333 22 1
Logic to print the above pattern:
- To iterate through rows, initialize an outer loop from N to 1 (where N is the total number of rows). Note that I have initialized the loop from N to 1 not from 1 to N as the pattern is in descending order hence it will help in the inner loop. As both 1 to N or N to 1 will iterate N times.
- To print numbers, initialize an inner loop from 1 to current_row_number (note that the current row number will be like n.., 4, 3...1 in decreasing order). Inside this loop print the value of current_row_number.
And you are done, lets implement this now.
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.