Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Common in Number Pattern Programs?
3.
What are various Number Pattern Programs in C?
3.1.
Half Pyramid of Numbers
3.2.
Inverted Half Pyramid
3.3.
Full Pyramid
3.4.
Pascal’s Triangle
3.5.
Floyd's Triangle
3.6.
Diamond Pattern
3.7.
Cross Pattern
3.8.
Binary Square Pattern
3.9.
Diamond Number Pattern
4.
Frequently Asked Questions
4.1.
In C, why do we employ conditional statements?
4.2.
How can I prevent getting trapped in an endless loop?
4.3.
What is a ternary operator in C?
5.
Conclusion
Last Updated: Sep 29, 2024
Easy

Number pattern programs in C

Author Nidhi Kumari
1 upvote

Introduction

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.

Introduction to Number Pattern programs in C

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(...);
  }
}
You can also try this code with Online C Compiler
Run Code

 

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;
}

You can also try this code with Online C Compiler
Run Code

 

Output

Enter the number of rows: 6
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
You can also try this code with Online C Compiler
Run Code

Inverted Half Pyramid

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter the number of rows: 6
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
You can also try this code with Online C Compiler
Run Code

Full Pyramid

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.

Full Pyramid

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter the number of rows: 5
         1 
      2 3 2 
    3 4 5 4 3 
  4 5 6 7 6 5 4 
5 6 7 8 9 8 7 6 5 
You can also try this code with Online C Compiler
Run Code

 

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter the number of rows: 5
    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 
You can also try this code with Online C Compiler
Run Code

 

Pascal’s Triangle

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.

 

Pascal's Triangle

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! )

Combinations of Pascal Triangle

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

Formula Derivation

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter the number of rows: 6
          1   
        1   1   
      1   2   1   
    1   3   3   1   
  1   4   6   4   1   
1   5   10   10   5   1
You can also try this code with Online C Compiler
Run Code

Floyd's Triangle

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.

Floyd's Triangle

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter the number of rows: 5
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
You can also try this code with Online C Compiler
Run Code

Diamond Pattern

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter the number of rows: 5
     1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1
You can also try this code with Online C Compiler
Run Code

 

Cross Pattern

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

1     1
 2   2 
  3 3  
   4   
  3 3  
 2   2 
1     1
You can also try this code with Online C Compiler
Run Code

Binary Square Pattern

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter the number of rows: 5
Enter the number of columns: 5
11111
00000
11111
00000
11111
You can also try this code with Online C Compiler
Run Code

Diamond Number Pattern

These patterns simply combine two or more different patterns.

Diamond Number Pattern

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;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter the value of n: 5
1
12
123
1234
12345
1234
123
12
1
You can also try this code with Online C Compiler
Run Code

Frequently Asked Questions

In C, why do we employ conditional statements?

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 trueVal = 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

 C vs C++.

           Getch in C

 Header Files.

 Identifiers and Keywords.

Ternary Operator in C

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Live masterclass