Number pattern 28 in C

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

Example

Input

Input N: 5

Output

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

To get the logic of above pattern have a look to the pattern carefully for a couple of minutes. The pattern consists of N rows (where N is the total number of rows to be printed). Each row contains exactly N - i + 1 columns (where i is the current row number).
Step-by-step descriptive logic:

  1. To iterate through rows, run an outer loop from 1 to N.
  2. To print columns, run an inner loop from N to i in decreasing order (where i is the current row number). Inside this loop print the value of j (where j is the current column number).

Program to print the given number pattern 1

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

#include <stdio.h>

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

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

    for(i=1; i<=N; i++)
    {
        // Logic to print numbers
        for(j=N; j>=i; j--)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Output

Enter N: 5
54321
5432
543
54
5

Logic to print the given number pattern 2

Once you get done with the first number pattern, then this wouldn't be much difficult as it is same as the pattern we just did except of trailing spaces. We only need to add the logic of printing spaces just before the number gets printed. If you hover mouse to the pattern above you can see or count total number of spaces per row and can easily think about the logic to print spaces. Here spaces are arranged in ascending order fashion i.e. first row contains 0 spaces, second row contains 1 space and so on last row contains 4 space. Total number of spaces per row is i - 1 (where i is the current row number).
Step-by-step descriptive logic to print spaces:

  1. To print space, run an inner loop from 1 to i - 1. Inside this loop print single blank space.

Program to print the given number pattern 2

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

#include <stdio.h>

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

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

    for(i=1; i<=N; i++)
    {
        // Logic to print spaces
        for(j=1; j<i; j++)
        {
            printf(" ");
        }

        // Logic to print numbers
        for(j=N; j>=i; j--)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols. as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>