Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern through C program.
Required knowledge
Logic to print the given number pattern
Before solving the given pattern have a close look to the pattern. You will find that the pattern internally can be divided in two patterns which can be shown below.
Now, what we need to do is combine the logic to print both these two patterns in single program. Logic of print both these patterns are simple and are explained in previous posts separately. You can check out these two posts to get the logic of printing first pattern and second pattern if you are unfamiliar with these two patterns.
Let’s combine them into single pattern.
Program to print the given pattern
/**
* C program to print the given number pattern
*/
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter rows: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Prints the first part of the pattern
for(j=1; j<=i; j++)
{
printf("%d", j);
}
// Prints the second part of the pattern
for(j=i-1; j>=1; j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Output
Enter rows: 5 1 121 12321 1234321 123454321
Happy coding 😉