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 N: 5


Required knowledge

Basic C programming, Loop

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


Screenshot

Number pattern program in C


Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment