The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Number Pattern - 11

Why Number Pattern

In most of the MNC interview questions such as in Zoho interview questions, IVTL Infoview interview questions, Amazon interview questions, Google interview questions, Infosys interview questions and even in Voonik interview questions, we come across number pattern questions with a probability of 1:5. We provide you a most commonly asked number pattern question for your interview preparation using C programming.

Lets code a C program to print following number pattern to have some fun

Number Pattern Logic in C Programming

C Program - Number Pattern

We challenge you, the programmer to do the following number pattern using C programming

Number Pattern Logic

step 1: Consider every single space as 0.

00001
00021
00321
04321
54321

step 2: From the above number Pattern, we can easily identify, this C program contains 3 for loop.

  • for loop represents every new line.
  • for loop for displaying 0.
  • for loop for displaying numbers.
c-number-pattern-1.c
#include <stdio.h>
int main()
{
int a, i, j;
for(a = 1; a <= 5; a++)
{
for(i = 5; i > a; i--)
{
printf("0");
}
for(j = a; j >= 1; j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
00001
00021
00321
04321
54321

Note:

Here we make use of three for loop. First for loop is to say the number of lines to be printed and to set destination value to print 0 and to initialize the value to print numbers. Second for loop is to print 0. Third for loop is to print Numbers.

C Program - Number Pattern Actual

Just replace 0 with an empty space.

c-number-pattern.c
#include <stdio.h>
int main()
{
int a, i, j;
for(a = 1; a <= 5; a++)
{
for(i = 5; i > a; i--)
{
printf(" ");
}
for(j = a; j >= 1; j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
1
   21
  321
 4321
54321

Note:

Here we make use of three for loop. First for loop is to say the number of lines to be printed and to set destination value to print space and to initialize the value to print numbers. Second for loop is to print space. Third for loop is to print Numbers.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report