# Mastering Number Patterns in C: A Programmer’s Guide

A number pattern is a series of numbers that are arranged in a specific order. In programming, number patterns form an essential part of algorithm thinking and problem-solving. Developing number patterns in C is a key component in learning the entire programming itself. With these patterns, developers and coders can learn different C programming principles.Â

Here, we will learn about number patterns in C++. Furthermore, we will also explore how to create appealing visual designs using loops and conditionals. There is a wide range of number patterns in C, which will be covered, from squares and triangles to intricate pyramids, diamond shapes and spirals. Each pattern will include a full description and C++ code to assist readers in understanding and implementing these engaging patterns.

Â *Geeksforgeeks.org

## Square Pattern: Pattern 1

One of the most fundamental number patterns is the square pattern. Printing a square with numbers ranging from 1 to N is required, where N is the total number of rows and columns.

### Example

#include <stdio.h>Â Â

int main() {Â Â

Â Â Â int N, i, j;Â Â

printf(“Enter the number of rows and columns: “);Â Â

scanf(“%d”, &N);Â Â

Â Â Â for (i = 1; i<= N; i++) {Â Â

Â Â Â Â Â Â for (j = 1; j <= N; j++) {Â Â

printf(“%d “, j);Â Â

Â Â Â Â Â Â }Â Â

printf(“\n”);Â Â

Â Â Â }Â Â

Â Â Â return 0;Â Â

}Â Â

### Output

Enter the number of rows and columns: 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

### Explanation

In the square pattern of the C programming number pattern, the programmer is asked the number of rows and columns. The inner loop handles the number of columns, and the outer loop manages the number of rows. Each time the inner loop iterates, the column number represented by the variable ‘j’ is shown.

## Hollow Square Pattern: Pattern 2

The hollow square pattern consists of printing a square of numbers with an asterisk (*) border and a number-filled inside.

### Example

#include <stdio.h>Â Â

int main() {Â Â

Â Â Â int N, i, j;Â Â

printf(“Enter the number of rows and columns: “);Â Â

scanf(“%d”, &N);Â Â

Â Â Â for (i = 1; i<= N; i++) {Â Â

Â Â Â Â Â Â for (j = 1; j <= N; j++) {Â Â

Â Â Â Â Â Â Â Â Â if (i == 1 || i == N || j == 1 || j == N)Â Â

printf(“* “);Â Â

Â Â Â Â Â Â Â Â Â elseÂ Â

printf(“%d “, j);Â Â

Â Â Â Â Â Â }Â Â

printf(“\n”);Â Â

Â Â Â }Â

Â Â Â return 0;Â Â

}Â Â

### Output

Enter the number of rows and columns: 5 * * * * *Â  * 1 2 3 *Â  * 1 2 3 *Â  * 1 2 3 *Â  * * * * *

### Explanation

This program generates a square of integers similar to the square pattern. Instead of writing the number, it displays an asterisk (*) if the current location (first row, final row, first column, or last column) is near the square’s edge.

## Pascalâ€™s Triangle Pattern: Pattern 3

Pascal’s Triangle is an array of binomial coefficients formed like a triangle. The two numbers directly above it sum to each number in the triangle.

### Example

#include <stdio.h>Â Â

int binomialCoeff(int n, int k) {Â Â

Â Â Â if (k == 0 || k == n)Â Â

Â Â Â Â Â Â return 1;Â Â

Â Â Â return binomialCoeff(n – 1, k – 1) + binomialCoeff(n – 1, k);Â Â

}Â Â

int main() {Â Â

Â Â Â int rows, i, j;Â Â

printf(“Enter the number of rows: “);Â Â

scanf(“%d”, &rows);Â Â

Â Â Â for (i = 0; i< rows; i++) {Â Â

Â Â Â Â Â Â for (j = 0; j <= i; j++) {Â Â

printf(“%d “, binomialCoeff(i, j));Â Â

Â Â Â Â Â Â }Â Â

printf(“\n”);Â Â

Â Â Â }Â Â

Â Â Â return 0;Â Â

}Â Â

### Output

Enter the number of rows: 5

1Â

1 1Â

1 2 1Â

1 3 3 1Â

1 4 6 4 1

### Explanation

We define a function called binomialCoeff, which uses a recursive procedure to calculate the binomial coefficient. The user is invited to enter the number of rows in the main programme. The program uses the binomialCoeff function to generate and output Pascal’s Triangle using two nested loops.

## Floydâ€™s Triangle: Pattern 4

Each row of Floyd’s Triangle has one more natural number than the row before it. It is a right-angled, triangular array of natural integers.

### Example

#include <stdio.h>

int main() {Â Â

Â Â Â int rows, i, j, num = 1;Â Â

printf(“Enter the number of rows: “);Â Â

scanf(“%d”, &rows);Â Â

Â Â Â for (i = 1; i<= rows; i++) {Â Â

Â Â Â Â Â Â for (j = 1; j <= i; j++) {Â Â

printf(“%d “, num);Â Â

Â Â Â Â Â Â Â Â Â num++;Â Â

Â Â Â Â Â Â }Â Â

printf(“\n”);Â Â

Â Â Â }Â Â

Â Â Â return 0;Â Â

}Â Â

### Output

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

### Explanation

The number of rows must be specified by the programmer. The program uses two stacked loops to produce and print Floyd’s Triangle. The variable num tracks the current number to be displayed.

## Pyramid Pattern: Pattern 5

The pyramid pattern involves printing an incremental pyramidal shape.

### Example

#include <stdio.h>Â Â

int main() {Â Â

Â Â Â int N, i, j;Â Â

printf(“Enter the number of rows: “);Â Â

scanf(“%d”, &N);Â Â

Â Â Â for (i = 1; i<= N; i++) {Â Â

Â Â Â Â Â Â for (j = 1; j <= N – i; j++) {Â Â

printf(” “);Â Â

Â Â Â Â Â Â }Â Â

Â Â Â Â Â Â for (j = 1; j <= i; j++) {Â Â

printf(“%d “, j);Â Â

Â Â Â Â Â Â }Â Â

printf(“\n”);Â Â

Â Â Â }Â Â

Â Â Â return 0;Â Â

}Â Â

### Output

Enter the number of rows: 5 Â Â Â Â 1 Â Â Â 1 2 Â Â 1 2 3 Â 1 2 3 4 1 2 3 4 5

### Explanation

The number of rows must be specified by the user. The first inner loop includes printing the gaps required for the numbers to be properly aligned. On each iteration of the outer loop, the second inner loop prints the integers 1 through ‘i’ in ascending order.

## Diamond Pattern: Pattern 6

The diamond pattern includes printing a mathematically generated diamond form.

### Example

#include <stdio.h>Â

int main() {Â Â

Â Â Â int N, i, j;Â Â

printf(“Enter the number of rows: “);Â Â

scanf(“%d”, &N);Â Â

Â Â Â for (i = 1; i<= N; i++) {Â Â

Â Â Â Â Â Â for (j = 1; j <= N – i; j++) {Â Â

printf(” “);Â Â

Â Â Â Â Â Â }Â Â

Â Â Â Â Â Â for (j = 1; j <= i; j++) {Â Â

printf(“%d “, j);Â Â

Â Â Â Â Â Â }Â Â

printf(“\n”);Â Â

Â Â Â }Â Â

Â Â Â for (i = N – 1; i>= 1; i–) {Â Â

Â Â Â Â Â Â for (j = 1; j <= N – i; j++) {Â Â

printf(” “);Â Â

Â Â Â Â Â Â }Â Â

Â Â Â Â Â Â for (j = 1; j <= i; j++) {Â Â

printf(“%d “, j);Â Â

Â Â Â Â Â Â }Â Â

printf(“\n”);Â Â

Â Â Â }Â Â

Â Â Â return 0;Â Â

}Â Â

### Output

Enter the number of rows: 5 Â Â Â Â 1 Â Â Â 1 2 Â Â 1 2 3 Â 1 2 3 4 1 2 3 4 5 Â 1 2 3 4 Â Â 1 2 3 Â Â Â 1 2 Â Â Â Â 1

### Explanation

The code for the pyramid pattern may be modified to generate the diamond pattern. Following the printing of the pyramid form, the lower half of the diamond is printed with a reverse loop.

## Spiral Pattern: Pattern 7

The spiral pattern consists of spirally printing a square matrix.

### Example:

#include <stdio.h>Â Â

#define N 4Â Â

void printSpiral(int matrix[N][N]) {Â Â

Â Â Â int i, k = 0, l = 0;Â Â

Â Â Â while (k < N && l < N) {Â Â

Â Â Â Â Â Â for (i = l; i< N; i++) {Â Â

printf(“%d “, matrix[k][i]);Â Â

Â Â Â Â Â Â }Â Â

Â Â Â Â Â Â k++;Â Â

Â Â Â Â Â Â for (i = k; i< N; i++) {Â Â

printf(“%d “, matrix[i][N – 1]);Â Â

Â Â Â Â Â Â }Â Â

Â Â Â Â Â Â N–;Â Â

Â Â Â Â Â Â if (k < N) {Â Â

Â Â Â Â Â Â Â Â Â for (i = N – 1; i>= l; i–) {Â Â

printf(“%d “, matrix[N – 1][i]);Â Â

Â Â Â Â Â Â Â Â Â }Â Â

Â Â Â Â Â Â Â Â Â N–;Â Â

Â Â Â Â Â Â }Â Â

if (l < N) {Â Â

Â Â Â Â Â Â Â Â Â for (i = N – 1; i>= k; i–) {Â Â

printf(“%d “, matrix[i][l]);Â Â

Â Â Â Â Â Â Â Â Â }Â Â

Â Â Â Â Â Â Â Â Â l++;Â Â

Â Â Â Â Â Â }Â Â

Â Â Â }Â Â

}Â Â

int main() {Â Â

Â Â Â int matrix[N][N] = {{1, 2, 3, 4},Â Â

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â {5, 6, 7, 8},Â Â

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â {9, 10, 11, 12},Â Â

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â {13, 14, 15, 16}};Â Â

printSpiral(matrix);Â Â

Â Â Â return 0;Â Â

}Â Â

### Output

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

### Explanation

The program has a function named printSpiral, which spirally prints the input matrix. It uses four loops, each representing a side of the square matrix, together with a while loop. The spiral limits are tracked using the loop variables k, l, and N.

## Other Number Patterns

### Rhombus Pattern

The Rhombus pattern is identical to the square pattern, with the exception that gaps must be added before each line, and their number reduces progressively as the rows increase.

#### Example

#include <stdio.h>Â

int main()Â

{Â

Â Â Â Â int rows = 5;

Â Â Â Â // first outer loop to iterate through each rowÂ

Â Â Â Â for (int i = 0; i < rows; i++) {Â

Â Â Â Â Â Â Â Â // first inner loop to print white spacesÂ

Â Â Â Â Â Â Â Â for (int j = 0; j < rows – i – 1; j++) {Â

Â Â Â Â Â Â Â Â Â Â Â Â printf(” “);Â

Â Â Â Â Â Â Â Â }Â

Â Â Â Â Â Â Â Â // second inner loop to print * star in each rowÂ

Â Â Â Â Â Â Â Â for (int k = 0; k < rows; k++) {Â

Â Â Â Â Â Â Â Â Â Â Â Â printf(“* “);Â

Â Â Â Â Â Â Â Â }Â

Â Â Â Â Â Â Â Â printf(“\n”);Â

Â Â Â Â }Â

Â Â Â Â return 0;Â

}

#### Output

Â Â * * * * *Â  Â  |Â  Â 1 2 3 4 5 Â  |Â  Â  A B C D E

Â  * * * * *Â  Â  |Â  Â 1 2 3 4 5Â  Â |Â  Â  A B C D E

Â Â * * * * *Â  Â  |Â  Â 1 2 3 4 5Â  Â |Â  Â  A B C D E

Â  * * * * *Â  Â  |Â  Â 1 2 3 4 5Â  Â |Â  Â  A B C D E

Â  * * * * *Â  Â  | Â  1 2 3 4 5Â  Â |Â  Â A B C D E

### Hourglass Pattern

The Hourglass pattern is a mixture of the inverted full pyramid and full pyramid patterns, although in the opposite meaning of the diamond design. We’re going to join them in utilising their tips.

#include <stdio.h>Â

int main()Â

{Â

Â Â Â Â int rows = 5;Â

Â Â Â Â // first outer loop to iterate each rowÂ

Â Â Â Â for (int i = 0; i < 2 * rows – 1; i++) {Â

Â Â Â Â Â Â Â Â // assigning comparatorÂ

Â Â Â Â Â Â Â Â int comp;Â

Â Â Â Â Â Â Â Â if (i < rows) {Â

Â Â Â Â Â Â Â Â Â Â Â Â comp = 2 * i + 1;Â

Â Â Â Â Â Â Â Â }Â

Â Â Â Â Â Â Â Â else {Â

Â Â Â Â Â Â Â Â Â Â Â Â comp = 2 * (2 * rows – i) – 3;Â

Â Â Â Â Â Â Â Â }Â

Â Â Â Â Â Â Â Â // first inner loop to print leading spacesÂ

Â Â Â Â Â Â Â Â for (int j = 0; j < comp; j++) {Â

Â Â Â Â Â Â Â Â Â Â Â Â printf(” “);Â

Â Â Â Â Â Â Â Â }Â

Â Â Â Â Â Â Â Â // second inner loop to print star *Â

Â Â Â Â Â Â Â Â for (int k = 0; k < 2 * rows – comp; k++) {Â

Â Â Â Â Â Â Â Â Â Â Â Â printf(“* “);Â

Â Â Â Â Â Â Â Â }Â

Â Â Â Â Â Â Â Â printf(“\n”);Â

Â Â Â Â }Â

Â Â Â Â return 0;Â

}

#### Output

* * * * * * * * * Â  | Â  1 2 3 4 5 6 7 8 9 Â  | Â  A B C D E F G H I

Â Â * * * * * * * Â  Â  | Â  Â  1 2 3 4 5 6 7 Â  Â  | Â  Â  A B C D E F G

Â Â Â Â * * * * * Â  Â  Â  | Â  Â  Â  1 2 3 4 5 Â  Â  Â  | Â  Â  Â  A B C D E

Â Â Â Â Â Â * * * Â  Â  Â  Â  | Â  Â  Â  Â  1 2 3 Â  Â  Â  Â  | Â  Â  Â  Â  A B CÂ

Â Â Â Â Â Â Â Â * Â  Â  Â  Â  Â  | Â  Â  Â  Â  Â  1 Â  Â  Â  Â  Â  | Â  Â  Â  Â  Â  A

Â Â Â Â Â Â * * * Â  Â  Â  Â  | Â  Â  Â  Â  1 2 3 Â  Â  Â  Â  | Â  Â  Â  Â  A B C

Â Â Â Â * * * * * Â  Â  Â  | Â  Â  Â  1 2 3 4 5 Â  Â  Â  | Â  Â  Â  A B C D E

Â Â * * * * * * * Â  Â  | Â  Â  1 2 3 4 5 6 7 Â  Â  | Â  Â  A B C D E F G

* * * * * * * * * Â  | Â  1 2 3 4 5 6 7 8 9 Â  | Â  A B C D E F G H I

##### Conclusion

For algorithm thinking and problem-solving abilities, the number pattern in C programming plays an important part. There are numerous patterns to create in C and C++ programming that will help users create the desired output to carry out their algorithms. Learning number patterns in C programming can offer a competitive edge in the world of computer applications.Â

If you want to enhance your career in the field of computer applications, you can pursue the Master Of Computer Applications (MCA) Online Degree Programme from Manipal University, Jaipur. With this course, you can be well-versed in cloud technology, machine learning, cloud infrastructure, application development and more. To create a balance between your personal/professional lives, this course allows you to attend classes from the comfort of your home so that you can learn at your own pace. To know more about this program, register with Jaro Education.Â Â