Number pattern 49 in C

Write a C program to print the given number pattern using loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern using C program.

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

These types of patterns are basically a combination of two or more number patterns. Here if you look carefully then you can easily notice that the given pattern is a combination of two patterns. Hence we can divide the pattern in two parts to make our task easier.

If you are following my previous posts, you might have noticed that these two patterns are explained in my previous post – Ascending order number pattern and descending order number pattern.
I recommend you to go though these two post before you get into this pattern.

Printing these two parts are really simple. Logic to print the first part.

  1. First upper part of the patter consists of N rows (where N is the total number of rows to be printed). Since the pattern is in ascending order hence the outer loop formation to iterate through rows will also be in ascending order i.e. for(i=1; i<=N; i++).
  2. Here each row contains exactly i columns (where i is the current row number). Inner loop formation to iterate through each columns is for(j=1; j<=i; j++). Print the current value of j inside this loop, to get the first part of the pattern.

Logic to print the second part of the pattern.

  1. Second lower part of the pattern consists of N – 1 rows. Run an outer loop to iterate through rows. Since the pattern is in descending order hence we need to run the loop in descending order. Therefore the outer loop formation will be for(i=N-1; j>=1; i–).
  2. Each row contains exactly i number of columns. Hence the inner loop formation to print columns will be for(j=1; j<=i; j++). To get the desired pattern print the current value of j inside this loop.

Lets, now combine the logic of both the parts in a single program.

Program to print the given number pattern 1

/**
 * C program to print the given number pattern
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    printf("Enter N: ");
    scanf("%d", &N);

    // Prints upper part of the pattern
    for(i=1; i<=N; i++)
    {
        for(j=1; j<=i; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    // Print lower part of the pattern
    for(i=N-1; i>=1; i--)
    {
        for(j=1; j<=i; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Output

Enter N: 5
1
12
123
1234
12345
1234
123
12
1

Logic to print the given number pattern 2

This pattern is exactly similar to the above pattern we just need to add spaces before the number gets printed. Here I am not getting into detailed explanation about the logic of this pattern. As we only need to add the code to print spaces.

Program to print the given number pattern 2

/**
 * C program to print the given number pattern
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    printf("Enter N: ");
    scanf("%d", &N);

    // Prints upper part of the pattern
    for(i=1; i<=N; i++)
    {
        // Prints trailing spaces
        for(j=N-1; j>=i; j--)
        {
            printf(" ");
        }

        for(j=1; j<=i; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    // Prints lower part of the pattern
    for(i=N-1; i>=1; i--)
    {
        // Prints trailing spaces
        for(j=i; j<N; j++)
        {
            printf(" ");
        }

        for(j=1; j<=i; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

If you love programming, try these interesting pattern of your own, based on the logic of above pattern. You just need to play with the spaces.

And many more…

Happy coding 😉