Number pattern 16 in C

Write a C program to print the given number pattern using loop. How to print the given number pattern of m rows and n columns using for loop in C programming. Logic to print the given number pattern using for loop in C program.

Required knowledge

Basic C programming, Loop

Logic to print the given patterns

Before all of these patterns confuse you, I recommend you to learn some basics of number pattern logic.

Once you get acquainted with basics of number pattern printing logic. If you look to these pattern carefully you will find that all of them are similar. Hence if you can decode the logic of any rest of them will be easy to code. Now, have take a minute and look to the below pattern carefully.

If you have looked carefully you can actually divide this pattern in two parts, let me show you.

Now, I hope that logic of whole pattern would have been cleared to you somewhat. If not let me explain the logic to print this pattern.

  1. To iterate through rows, run an outer loop from 1 to rows (where rows is total rows to be printed).
  2. To print first part of pattern, run an inner loop from columns to columns - current_row (where columns is total columns to be printed). Inside this loop print the value of current column.
  3. To print the second part of pattern, run another inner loop from 1 to columns - current_row. Inside this loop print the value of rows - current_row + 1.

Lets implement this...

Note: Logic to all remaining patterns are similar so I won't be explaining logic of remaining three patterns. But source code of all the three patters are given.

Program to print number pattern 1

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

#include <stdio.h>

int main()
{
    int rows, cols, i, j;

    /* Input rows and columns from user
     */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

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

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

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
Enter number of columns: 5
55555
54444
54333
54322
54321

Program to print number pattern 2

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

#include <stdio.h>

int main()
{
    int rows, cols, i, j;

    /* Input rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

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

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

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
Enter number of columns: 5
55555
44445
33345
22345
12345

Program to print number pattern 3

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

#include <stdio.h>

int main()
{
    int rows, cols, i, j;

    /* Input rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

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

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

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
Enter number of columns: 5
54321
54322
54333
54444
55555

Program to print number pattern 4

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

#include <stdio.h>

int main()
{
    int rows, cols, i, j;

    /* Input rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

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

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

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
Enter number of columns: 5
12345
22345
33345
44445
55555

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>