One fundamental numeric sequence is the square pattern. Generating a square grid with numbers ranging from 1 to N is necessary, where N represents the total count 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 this instance, the user is prompted to indicate the quantity of rows and columns available. As the inner loop controls the quantity of columns, the outer loop regulates the number of rows. With each iteration of the inner loop, the value of 'j' representing the column number is displayed.
Pattern 2: Pyramid Pattern
The pyramid pattern involves displaying a pyramid-like structure with increasing levels.
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 user is required to input the total number of rows. The initial inner loop handles the task of printing the spaces required for aligning the numbers correctly. Within each cycle of the outer loop, the second inner loop displays the numbers incrementally from 1 to 'i'.
Pattern 3: Diamond Pattern
The diamond pattern involves outputting a diamond shape formed by numbers.
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 designed for generating the pyramid pattern can be expanded to produce the diamond pattern. Printing the lower section of the diamond involves using a backward loop following the completion of the pyramid pattern.
Pattern 4: Pascal's Triangle
A triangular array composed of binomial coefficients is referred to as Pascal's Triangle. Each number in the triangle is the sum of the two numbers directly above it.
#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 establish a function named binomialCoeff which utilizes a recursive approach to calculate the binomial coefficient. The user is asked to input the number of rows within the primary program. This function, binomialCoeff, is then utilized by the program to generate and display Pascal's Triangle through the implementation of two nested loops.
Pattern 5: Floyd's Triangle
Floyd's Triangle is a triangular array of natural numbers where each successive row contains an additional natural number compared to the previous row. This arrangement forms a right-angled triangle of numbers.
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 user needs to input the quantity of rows for the program. By employing a pair of nested loops, Floyd's Triangle is created and displayed. The variable num keeps track of the present number that is being printed.
Pattern 6: Spiral Pattern
The spiral design involves printing a square matrix in a spiral manner.
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 software includes a function named displaySpiral which sequentially outputs the given matrix in a spiral pattern. It utilizes four for loops to iterate through each side of the square matrix, along with a while loop. The boundaries of the spiral are monitored by the loop counters k, l, and N.
Pattern 7: Hollow Square Pattern
Printing a square of numbers enclosed by an asterisk (*) border and filled with numbers characterizes the hollow square pattern.
#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 code generates a grid of whole numbers resembling a square design. Instead of displaying the numbers, it replaces them with an asterisk (*) if the current position aligns with the square's perimeter (top row, bottom row, left column, or right column).
Conclusion:
Finally, C programs focusing on number patterns provide a valuable opportunity to enhance programming skills and problem-solving abilities. This article explored a range of patterns, starting from basic ones like square, pyramid, and diamond patterns to more intricate ones like Pascal's triangle, Floyd's triangle, hollow square pattern, and Fibonacci series. By understanding the syntax, implementing the code, and analyzing the outcomes, we delved deeper into the ways numbers can be arranged and manipulated to create visually appealing patterns.
These applications foster innovation and logical thinking while aiding learners in grasping loops, conditionals, and recursive functions more deeply. Developers can establish a strong foundation for addressing progressively complex programming challenges and enhance their comprehension of the language's capabilities through mastery of number pattern programs in C. To enhance your programming skills comprehensively, persist in exploring and testing various number patterns.