A number pattern is a group of numbers placed in a particular sequence. Numerous programmers usually recommend practising Pattern programs since they improve the ability to create logic while utilising Flow Control Statements. Further, it improves logical reasoning skills.
This article will give beginners and intermediate programmers a collection of number pattern programs in C to practise.
What is Common in Number Pattern Programs?
The common elements in Number Pattern Programs are Conditional Statements. We can create patterns by combining Number Patterns with conditional loops and syntax.
Let's look closely at the pattern interpretation of number pattern programs in C.
The first and second groups of the C pattern programme are the conditions for loops and the print statement, respectively.
for(condition of the outer loop){
for(condition of the inner loop){
print(...);
}
}
Consider the advice given below with a level of caution, as it may not fit every situation.
The loops' conditions can be created as follows:
If the number of elements in the rows grows, the outer loop will start at i = 1. Otherwise, it will start at i = row or i = n, where n can be any integer.
The inner loop will start at j = 1 if the elements in each row are growing. Otherwise, it will start at j = row, j = i, or j = n, where n can be any integer.
The above approach often works with patterns, but we should constantly cross-check the loop's conditions. In other words, to create a number pattern programs in C, consider the following points:
Developing number pattern programs in C is simple if you are familiar with loops like the for and while loops.
To display number pattern programs in C, most of the time, two nested loops are required.
The outer loop determines the number of rows, while the inner loop decides what will go in each row.
What are various Number Pattern Programs in C?
Let’s discuss the various Number Pattern Programs in C one by one.
Half Pyramid of Numbers
The user can specify the number of rows to print the half-pyramid of numbers in the following C program, and it will show the desired output on the screen.
Code
#include <stdio.h>
int main()
{
int n;
//User Input
printf("Enter the number of rows: ");
scanf("%d",&n);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}
This pattern involves an upside-down(inverted) half-pyramid. The idea is the same; however, because the size of each row decreases from n to 1, the outer loop moves in the other direction.
Code
#include <stdio.h>
int main()
{
int n;
// User Input
printf("Enter the number of rows: ");
scanf("%d", &n);
//outer loop moving in the opposite direction
for (int i = n; i >= 1; --i)
{
for (int j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}
There are numerous methods to create a strategy to tackle such a pattern, but let's look at one that is very simple. Divide the Full pyramid pattern into two different patterns.
Code
#include<stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for (int i = 1; i <= n; i++) {
//Pattern 1 starts here
//first, we print all the blank spaces n-i times
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
//The elements that can be seen in each pattern row and
// whose values range from i to 2*i-1 are then printed.
for (int j = i; j < 2 * i; j++) {
printf("%d ", j);
}
//Pattern 2 starts here
//each row begins with 2*(i-1) and then decremented i - 1 times
int k = 2 * (i - 1);
for (int j = 1; j <= i - 1; j++) {
printf("%d ", k--);
}
//after completing each row, we print a new line.
printf("\n");
}
return 0;
}
Here is another example of a Full pyramid number pattern program in C.
Code
#include<stdio.h>
int main() {
int n;
// User Input
printf("Enter the number of rows: ");
scanf("%d", & n);
// Outer for loop runs n times (the number of rows to print)
for (int i = 1; i <= n; i++) {
// The number of spaces at the front will be n - i
for (int j = n; j > i; j--) {
printf(" ");
}
// Print the numbers in the current row from 1 to i
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Binomial coefficients are arranged in a triangular form in Pascal's triangle. Each row is numbered from the left, starting with j = 0, with the top row having the number i =0 ( the 0th row). The unique element at the 0th row is 1. For the rest of the numbers, the sum of two numbers located in the previous row and precisely at the top of the current cell is used to get each number.
The formula of combinations, often known as "n choose k" or the number of combinations of k elements from n elements, is used to calculate each row element in a Pascal triangle.
C(n, k) = n! / (n-k) ! * k!
The following pattern appears if we are in row i and column j:
C(i,j) = i! / ( (i-j)! * j! )
Let's now try to get C(i,j) using C(i, j-1):
C(i, j) = i! / ( (i-j)! * j! )
C(i, j-1) = i! / ( (i - j + 1)! * (j-1)! )
From the two expressions above, we can get the following expression.
C(i, j) = C(i, j-1) * (i - j + 1) / j
Therefore, in O(1) time, C(i, j) can be derived from C(i, j-1). As a result, we can maintain a variable whose value can be changed to show the pattern.
Code
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
for(int j = 1; j <= n-i; j++)
{
printf(" ");
}
int C = 1; // used to represent C(i, i)
for (int j = 1; j <= i; j++)
{
printf("%d ", C);
C = C * (i - j) / j;
}
printf("\n");
}
return 0;
}
A right-angled triangle with consecutive natural numbers is called Floyd's triangle. The number in Floyd's triangle begins with 1 in the top left corner and fills the specified rows one at a time using the numbers.
The only tricky part of this pattern is keeping a separate variable(num) that may be updated and shown during each loop iteration.
Code
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
int num = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; ++j)
{
printf("%d ", num);
++num;
}
printf("\n");
}
return 0;
}
The user can input the desired number of rows to print the Diamond pattern of numbers in the following Number pattern program in C.
Code
#include<stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", & n);
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int k = 1; k < (i * 2); k++) {
printf("%d", k);
}
printf("\n");
}
for (int i = 4; i >= 1; i--) {
for (int j = n; j > i; j--) {
printf(" ");
}
for (int k = 1; k < (i * 2); k++) {
printf("%d", k);
}
printf("\n");
}
return 0;
}
The user can enter the desired number of rows to print the Cross pattern of numbers in the following C program.
Code
#include<stdio.h>
int main()
{
int i, j, num = 1;
int m[7][7] = {0};
for (i = 1; i <= 7; i++)
{
for (j = 1; j <= 7; j++)
if (j == i || 8 - i == j)
m[i - 1][j - 1] = num;
if (i < 4)
num++;
else
--num;
}
for (i = 0; i < 7; i++)
{
for (j = 0; j < 7; j++)
{
if (m[i][j] == 0)
printf(" ");
else
printf("%d", m[i][j]);
}
printf("\n");
}
return 0;
}
The user can enter the desired number of rows and columns to print the Binary Square Number pattern programs in C.
Code
#include <stdio.h>
int main()
{
int rows, cols, i, j;
/* Input rows and columns from user */
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
// Print 1 if the current row is odd
if (i % 2 == 1)
{
printf("1");
}
else
{
printf("0");
}
}
printf("\n");
}
return 0;
}
These patterns simply combine two or more different patterns.
Code
#include <stdio.h>
int main()
{
int i, j, n;
printf("Enter the value of n: ");
scanf("%d", &n);
// Prints first(upper) part of the pattern
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d", j);
}
printf("\n");
}
// Print second(lower) part of the pattern
for (i = n - 1; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
In C programming, conditional statements are employed to make decisions based on the conditions. When no condition encloses the statements, conditional statements are executed sequentially. The execution flow may vary if a condition is added to a block of statements depending on the outcome of the condition's analysis.
How can I prevent getting trapped in an endless loop?
Ensure that the loop has at least one instruction that updates the comparison variable's value. (That is, the variable you employ in the condition statement of the loop.) Also, Never rely on the user to determine the terminating condition.
What is a ternary operator in C?
A ternary operator is a shorthand way of writing if-else statements. It takes three operands (one of its kind). The syntax of the ternary operator is:
Val = condition? expr1: expr2
If the condition is true, Val = expr1, else Val = expr2.
Conclusion
We discussed some critical number pattern programs in C. Developing a number pattern program in C is simple if you are familiar with loops like the for and while loops. To display a number pattern program in C, at least two nested loops are required.
We hope this blog has helped you. We recommend you visit our articles on different topics of C language, such as