Write a C program to print the given number pattern using for loop. How to print the given pattern of m rows and n columns using for loop in C programming. Logic to print the given number pattern using loop in C program.
Example
Input
Input N: 5
Output
555555555 544444445 543333345 543222345 543212345 543222345 543333345 544444445 555555555
Required knowledge
Logic to print the given number pattern
Before you get into this confusing but interesting number pattern I recommend you to get yourself acquainted with the basics of number printing, learn the basic logic to print number pattern.
Once you get acquainted with basics of number pattern printing, have a careful eye on to above pattern. Let me help you, to make things easier lets divide this entire pattern into two big parts and six smaller pieces. The first upper half contains three parts
--------- 5-------- 54------- 543------ 5432----- --------- --------- --------- ---------
555555555 -4444444- --33333-- ---222--- ----1---- --------- --------- --------- ---------
--------- --------5 -------45 ------345 -----2345 --------- --------- --------- ---------
And the lower half of the pattern also contains three separate parts.
--------- --------- --------- --------- --------- 5432----- 543------ 54------- 5--------
--------- --------- --------- --------- --------- ----2---- ---333--- --44444-- -5555555-
--------- --------- --------- --------- --------- -----2345 ------345 -------45 --------5
Now, both upper and lower half’s of the pattern would be printed separately in two separate outer loops. Considering the first upper half of the pattern. The logic to print upper half of the pattern:
- To iterate through rows, initialize a loop from N till 1. (Note: We can also use loop from 1 to N but we have used it in decrementing order as the upper half is in decrementing order)
- To print the first part of upper half, run an inner loop from N to 1 and print the current column number.
- To print the second part of the upper half, run another inner loop from 1 to i*2-1 which is the total number of columns per row in this part. Inside this loop print the current row number.
- To print the third part of the upper half, run another inner loop from current_row till N and print column number inside this loop.
Now, once you have printed the upper half of the pattern its time to get into lower half of the pattern. Logic to print the lower half of the pattern is
- To iterate through rows, run a loop from 1 to N.
- To print the first inner part of the lower half, run a loop from N to 1. Inside this loop print the column number.
- To print the second inner part of the lower half, run another loop from 1 to i*2-1 which is the total number of columns per row in this part. Inside this loop print current row number + 1.
- Finally to print the last inner part of lower half, run another loop from current row + 1 till N. Inside this loop print the current column number.
After this much of fuss finally you are done, lets now implement both logic into one program.
Program to print the given number pattern
/**
* C program to print number pattern
*/
#include <stdio.h>
int main()
{
int N, i, j;
printf("Enter N: ");
scanf("%d", &N);
// First upper half of the pattern
for(i=N; i>=1; i--)
{
// First inner part of upper half
for(j=N; j>i; j--)
{
printf("%d", j);
}
// Second inner part of upper half
for(j=1; j<=(i*2-1); j++)
{
printf("%d", i);
}
// Third inner part of upper half
for(j=i+1; j<=N; j++)
{
printf("%d", j);
}
printf("\n");
}
// Second lower half of the pattern
for(i=1; i<N; i++)
{
// First inner part of lower half
for(j=N; j>i; j--)
{
printf("%d", j);
}
// Second inner part of lower half
for(j=1; j<=(i*2-1); j++)
{
printf("%d", i+1);
}
// Third inner part of lower half
for(j=i+1; j<=N; j++)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Output
Enter N: 5 555555555 544444445 543333345 543222345 543212345 543222345 543333345 544444445 555555555
Happy coding 😉
Recommended posts
- Number pattern programming exercises index.
- Star patterns programming exercises index.
- Loop programming exercises index.
- Recommended patterns –
11111 11111 11111 11111 11111
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
12345 21234 32123 43212 54321
12345 23451 34521 45321 54321
12345 23455 34555 45555 55555
55555 54444 54333 54322 54321
11111 22222 33333 44444 55555