# Number Pattern Programs in C

Number pattern programs in C | In the previous C program examples, we developed many pattern programs in C, star pattern in C, diamond pattern in C. Now, let us develop number pattern programs in C.

While developing pattern programs, everything we talk about is in the terms of rows and columns. We treat patterns as a two-dimensional representation with rows and columns. To represent a two-dimension we need nested loops where rows are manipulated using an outer loop, and columns are manipulated using an inner loop.

## Square Number Pattern Programs in C

Number Pattern Programs in C – 1 | Write a program to display the below square number pattern in C.

Sample Input and Output:-

N = 4
1234
1234
1234
1234

N = 6
123456
123456
123456
123456
123456
123456

In this number pattern in C, for the input N, there are N rows and N columns. Each and every row goes from 1 to N. For example:- When input N = 4 then it contains 4 rows and 4 columns. In the 1st row, it contains 1 to N, similarly, in the 2nd row it contains 1 to N, and so on.

Therefore the outer loop will repeat from 1 to N where N is the input size. The inner loop will also repeat from 1 to N, and the inner loop number will be displayed.

``````#include<stdio.h>
int main()
{
int n;

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

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

return 0;
}``````

Number Pattern Programs in C – 2 | Write a program to display the below square number pattern in C.

Sample Input and Output:-

N = 4
1111
2222
3333
4444

N = 6
111111
222222
333333
444444
555555
666666

In this number pattern program in C, for input N there are N rows and N columns. Each row contains an N row number. For example:- When n = 4 then it contains 4 rows and 4 columns. Each row contains an N column and is filled with the current row number. In the 1st row, there are four 1, in the 2nd row there are four 2, and so on.

The outer loop goes from 1 to N, and the inner loop also goes from 1 to N. To achieve this number pattern in C, In each iteration of the inner loop the current row number will be displayed to the screen.

``````#include<stdio.h>
int main()
{
int n;

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

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

return 0;
}
``````

Number Pattern Programs in C – 3 | Write a program to display the below square number pattern in C.

Sample Input and Output:-

N = 4
1111
1122
1333
4444

N = 7
1111111
1111122
1111333
1114444
1155555
1666666
7777777

In this number pattern program in C, for input N there are N rows and N columns. In the ith row when column number j <= (N – i) then it contains 1 else it contains i (row-number).

For example:- when input n = 4, then it contains 4 rows and 4 columns. In the 1st row, it contains all 1’s. In the 2nd row, from columns 1 to 5-2 = 3 it contains 1, else it contains row number 2. Similarly in the 3rd rows, from columns 1 to 5-3 = 2 it contains 1, else it contains row number 3, and so on.

``````#include<stdio.h>
int main()
{
int n;

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

for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(j <= n-i) printf("%d", 1);
else printf("%d", i);
}
printf("\n");
}

return 0;
}``````

## Half Pyramid Number Pattern Programs in C

Number Pattern Programs in C – 4 | Write a program to display the below half pyramid number pattern in C.

Sample Input and Output:-

N = 5
1
21
321
4321
54321

N = 7
1
21
321
4321
54321
654321
7654321

In this half pyramid number pattern program in C, for the input N, there are N rows but the number of columns in a particular row depends upon the current row number. For example in the 1st row, there is 1 column, in the 2nd row there are 2 columns, in the 3rd row there are 3 columns, and so on.

Since this number pattern in C contains N rows, therefore, the outer loop will go from 1 to N. But in the ith row, the inner loop will go from i to 1. Example:- for input n = 5, in the 1st-row inner loop, goes from 1 to 1, in the 2nd-row inner loop goes from 2 to 1, in the 3rd-row inner loop goes from 3 to 1, and so on.

``````#include<stdio.h>
int main()
{
int n;

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

for(int i = 1; i <= n; i++)
{
for(int j = i; j > 0; j--)
{
printf("%d", j);
}
printf("\n");
}

return 0;
}``````

## Number Pattern Programs in C for Floyd’s Triangle

Number Pattern Programs in C – 5 | Write a program to display the below Floyd’s Triangle number pattern in C.

Sample input and outputs:-

N = 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

N = 9
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45

The Floyd’s Triangle pattern starts from 1 and increases by 1 repeatedly. For the input N, it contains N rows, and every ith row contains i number of columns. Therefore the outer loop will go from 1 to N, but the inner loop will go from 1 to i in the ith row.

We will take a temporary variable “a”, and initialized it with 1. After each iteration of the inner loop, it will be increased by 1. This variable value will be displayed on the screen. To maintain format we will use %3d. Read more:- Output format using printf() in C.

``````#include<stdio.h>
int main()
{
int n, a = 1;

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

for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
printf("%3d", a++);
}
printf("\n");
}

return 0;
}``````

### Similar Number Pattern Program in C

Number Pattern Programs in C – 6 | Note:- The below pattern is not Floyd’s Triangle. Write a program to display the below number pattern in C.

Sample input and outputs:-

N = 5
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15

N = 7
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16
22 23 24 25 26 27 28

In this number pattern in C, for the input N, it contains N rows, but the number of columns in a given row depends upon the current row number. In the ith row, this number pattern contains i number of columns.

In this number pattern in C, when row-number is odd then digits are in increasing order, but when row-number is even then digits are in decreasing order. To print this pattern, we need to take two temporary variables “a”, and “b”.

``````#include<stdio.h>
int main()
{
int n;
int a = 1, b;

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

for(int i = 1; i <= n; i++)
{
b = a + i - 1;
for(int j = 1; j <= i; j++, a++)
{
if(i % 2 == 1) printf("%3d", a);
else printf("%3d", b--);
}
printf("\n");
}

return 0;
}``````

## Full Pyramid Number Pattern Programs in C

Number Pattern Programs in C – 7 | Write a program to display the below full pyramid number pattern in C.

Sample input and outputs:-

``````N = 5
1
121
12321
1234321
123454321``````
``````N = 7
1
121
12321
1234321
123454321
12345654321
1234567654321``````

In this number pattern program in C, for input N there are N rows. Each ith row contains N+i number of columns, among them first N-i columns are filled with spaces, next i columns are filled from 1 to I, and next (i-1) columns are filled from (i-1) to 1.

Example:- when input n = 7, in the 5th row first 7-5 = 2 columns are filled with space, next 5 columns are filled from 1 to 5, next 5-1 = 4 columns are filled from 4 to 1.

``````#include<stdio.h>
int main()
{
int n, a;

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

for(int i = 1; i <= n; i++)
{
for(int s = 1; s <= n-i; s++)
printf(" "); // space

for(int j = 1; j <= (2*i-1); j++)
{
if(j < i) printf("%d", j);
else if(j == i)
{
printf("%d", j);
a = j;
}
else printf("%d", --a);
}
printf("\n");
}

return 0;
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!