55555 54444 54333 54322 54321
/**
* C program to print number pattern
* www.atnyla.com
*/
#include <stdio.h>
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;
}
Enter number of rows: 5
Enter number of columns: 5
55555
54444
54333
54322
54321
Basic C programming, Loop
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.
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
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.