Number Patterns
Number pattern 37 in C
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.
Example
Input
Input N: 5
Output
1 121 12321 1234321 123454321
Required knowledge
Basic C programming, Loop
Logic to print the given number pattern
1 121 12321 1234321 123454321
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.
1 12 123 1234 12345
1 21 321 4321
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