Number pattern 42 in C

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

Example

Input

Input N: 5

Output

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

The above pattern might seem confusing at first go. But trust me solving this will definitely boost your logical and reasoning thinking ability. Take your time and observe the logic of the pattern. If its getting difficult to solve the pattern let me help you to solve the pattern. You can easily notice following things about the pattern.

  1. It contains total N rows (where N is the total number of rows to be printed).
  2. Each row contains total i number of columns (where i is the current row number).
  3. Now when you look to the number of each row they are in special format. Number of each row starts with the current row number i.e. i and they continue to increment in special format. The differences between columns are 4, 3, 2, 1. i.e. If the first term of column is 5 then next will be 5+4=9, 9+3=12, 12+2=14 and so on...

Let's code down the solution.

Program to print the given number pattern

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

#include <stdio.h>

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

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

    for(i=1; i<=N; i++)
    {
        diff = N-1; // Initialize difference to total rows - 1
        value = i;  // Initialize value to the current row number
        for(j=1; j<=i; j++)
        {
            printf("%-3d", value);

            value += diff; // Computes the next value to be printed
            diff--;        // Decrements the difference
        }

        printf("\n");
    }

    return 0;
}

Output

Enter rows: 5
1
2  6
3  7  10
4  8  11 13
5  9  12 14 15

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>