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.

Input N: 5

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


    return 0;

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


Number pattern 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