Number pattern 40 in C

Write a C program to print the given number pattern using 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 pattern

On first look the pattern may seem little difficult to print out. Lets make things little easier and dissect the given pattern in two internal parts. Between these two patterns spaces are printed in decreasing order i.e first row contains 8, second row contains 6 spaces and so on last row contains 0 spaces. Loop formation to print spaces will be for(j=i*2; j<N*2; j++).
Loop to print the above two pattern will be similar and simple. It has also been discussed in one of my earlier post. Lets combine everything in single program.

Program to print the given pattern

 * C program to print the given number pattern

#include <stdio.h>

int main()
    int i, j, N;

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

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

        // Prints spaces between two parts
        for(j=i*2; j<N*2; j++)
            printf(" ");

        // Prints second part of the pattern
        for(j=i; j>=1; j--)
            printf("%d", j);


    return 0;

Enter rows: 5
1        1
12      21
123    321
1234  4321


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