Number Pattern Program In C


In this article, you are going to see 15 different number pattern programs in C. These programs are very easy to understand, steps and complete code is given for each program.

    Table Of Contents

  1. Number Square pattern in C
  2. Number Hollow square pattern
  3. Left triangle Pattern program in C
  4. Right triangle Pattern program in C
  5. Number Left Down triangle
  6. Number Right Down triangle
  7. Pyramid number pattern in C
  8. Reverse pyramid number pattern in C
  9. Diamond number pattern in C
  10. Hollow Diamond number pattern in C
  11. Hourglass number pattern
  12. Pascal number pattern
  13. Heart number pattern
  14. Plus number pattern
  15. Cross number pattern

1. Number Square pattern in C

Number square pattern is a simple pattern in which we print numbers in a square shape. The number of rows and columns are equal in square pattern.

11111
22222
33333
44444
55555

Here are the steps you can follow to print this pattern in C.

  1. Take a size of square pattern.
  2. Run a for loop from 1 to size. This will tell how many rows you need to print.
  3. Insite this create another internal loop from 1 to size. This prints the rows.
  4. Print the value of i in each row.
  5. Print a new line after each row.
#include <stdio.h>

int main() {
  // take a size
  // you can take user input
  int size = 5;
  // external loop
  for (int i = 1; i <= size; i++) {
    // internal loop
    for (int j = 1; j <= size; j++) {
      printf("%d", i);
    }
    printf("\n");
  }
  return 0;
}

Output:

11111
22222
33333
44444
55555

2. Number Hollow square pattern

Number hollow square pattern is same as square pattern but here we print numbers only at the edges of the square.

11111
2   2
3   3
4   4
55555

Here are the steps you can follow to print this pattern in C.

  1. Set size of your pattern or can also take user input.
  2. Execute a nested loop where external loop prints the rows and internal loop prints the columns.
  3. In the internal loop check if the value of i is 1 or size or if the value of j is 1 or size. If any of the condition is true then print the value of i.
  4. Else print a space.
  5. Print a new line after each row.
#include <stdio.h>

int main() {
  // take a size
  // you can take user input
  int size = 5;
  // external loop
  for (int i = 1; i <= size; i++) {
    // internal loop
    for (int j = 1; j <= size; j++) {
      if (i == 1 || i == size || j == 1 || j == size) {
        printf("%d", i);
      } else {
        printf(" ");
      }
    }
    printf("\n");
  }
  return 0;
}

Output:

11111
2   2
3   3
4   4
55555

3. Left triangle Pattern program in C

Left triangle pattern is a simple pattern in which we print numbers in a triangle shape whose tail is on the left side.

    1
   22
  333
 4444
55555

Here are the steps to create this pattern in C.

  1. Choose size of your pattern.
  2. Execute a nested loop where external loop prints the rows and inside it we have 2 internal loops.
  3. The first internal loop prints the spaces before the numbers.
  4. The second internal loop prints the numbers.
#include <stdio.h>

int main() {
  // set size
  // or take user input
  int size = 5;

  for (int i = 0; i < size; i++) {
    // printing spaces before numbers
    for (int j = 1; j < size-i; j++) {
      printf(" ");
    }
    // printing numbers
    for (int k = 0; k <= i; k++) {
      printf("%d", k+1);
    }
    printf("\n");
  }
  return 0;
}

Output:

    1
   22
  333
 4444
55555

4. Right triangle Pattern program in C

Right triangle pattern is a simple pattern in which we print numbers in a triangle shape whose tail is on the right side.

1
22
333
4444
55555

Given below is complete C program to print this pattern.

#include <stdio.h>

int main() {
  // set size
  // or take user input
  int size = 5;

  for (int i = 0; i < size; i++) {
    // printing numbers
    for (int j = 0; j <= i; j++) {
      printf("%d", j+1);
    }
    printf("\n");
  }
  return 0;
}

Output:

1
22
333
4444
55555

5. Left down triangle Pattern program in C

Left down triangle pattern is another variation of triangle pattern. It is water image of left triangle pattern.

55555
 4444
  333
   22
    1

Given below is the complete code to print this pattern in C.

#include <stdio.h>

int main() {
  // set size
  // or take user input
  int size = 5;

  for (int i = 0; i < size; i++) {
    // printing spaces before numbers
    for (int j = 0; j < i; j++) {
      printf(" ");
    }
    // printing numbers
    for (int k = 0; k < size - i; k++) {
      printf("%d", size - i);
    }
    printf("\n");
  }
  return 0;
}

Output:

55555
 4444
  333
   22
    1

6. Right down triangle Pattern program in C

Right down triangle pattern is another variation of triangle pattern. It is water image of right triangle pattern.

55555
4444
333
22
1

Complete code for right down triangle pattern in C is given below.

#include <stdio.h>

int main() {
  // size of the triangle
  int size = 5;

  for (int i = 0; i < size; i++) {
    // printing numbers
    for (int j = 0; j < size-i; j++) {
      printf("%d", size - i);
    }
    printf("\n");
  }
  return 0;
}

Output:

55555
4444
333
22
1

7. Pyramid Pattern program in C

Pyramid pattern is very famous pattern. It is occasionally asked in interviews.

    1
   123
  12345
 1234567
123456789

Follow the steps given below to create pattern number pattern program in C:

  1. We have taken size of the pyramid as 5.
  2. First create a nested loop where the outer loop runs from 0 to size-1 to print the rows.
  3. Inside the outer loop, there will be two loop loops, first loop will print spaces before numbers and second loop will print numbers.
  4. Numbers will be printed from 1 to 2*i+1.
#include <stdio.h>

int main() {
  int size = 5;

  // creating pyramid
  for (int i = 0; i < size; i++) {
    // print spaces before numbers
    for (int j = 0; j < size-i-1; j++) {
      printf(" ");
    }
    // print numbers
    for (int k = 0; k < 2*i+1; k++) {
      printf("%d", k+1);
    }
    printf("\n");
  }
  return 0;
}

Output:

    1
   123
  12345
 1234567
123456789

8. Inverted Pyramid Pattern program in C

Inverted pyramid pattern is water image of pyramid pattern.

123456789
 1234567
  12345
   123
    1

Given below is the complete code to print inverted pyramid pattern in C.

#include <stdio.h>

int main() {
  int size = 5;

  // creating pyramid
  for (int i = 0; i < size; i++) {
    // print spaces
    for (int j = 0; j < i; j++) {
      printf(" ");
    }
    // print number
    for (int k = 0; k < 2 * (size - i) - 1; k++) {
      printf("%d", k+1);
    }
    printf("\n");
  }
  return 0;
}

Output:

123456789
 1234567
  12345
   123
    1

9. Diamond Bumber Pattern program in C

Diamond number pattern has a shape of rhombus made up of numbers.

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

Here is complete code to create number number diamond pattern in C:

#include <stdio.h>

int main() {
  int size = 5;
  int num = 1;

  // upside pyramid
  for (int i = 1; i <= size; i++) {
    // printing spaces
    for (int j = size; j > i; j--) {
      printf(" ");
    }
    // printing number
    for (int k = 0; k < i * 2 - 1; k++) {
      printf("%d", num++);
    }
    // set the number to 1
    num = 1;
    printf("\n");
  }
  // downside pyramid
  for (int i = 1; i <= size - 1; i++) {
    // printing spaces
    for (int j = 0; j < i; j++) {
      printf(" ");
    }
    // printing number
    for (int k = (size - i) * 2 - 1; k > 0; k--) {
      printf("%d", num++);
    }
    // set num to 1
    num = 1;
    printf("\n");
  }
  return 0;
}

Output:

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

10. Hollow Diamond Pattern program in C

Hollow diamond number pattern is a diamond pattern with hollow spaces. These have numbers only at the edges.

    1
   1 2
  1   2
 1     2
1       2
 1     2
  1   2
   1 2
    1

Here is the complete code to create hollow diamond number pattern in C:

#include <stdio.h>

int main() {
  int size = 5, num = 1;
  // upside pyramid
  for (int i = 1; i <= size; i++) {
    // printing spaces
    for (int j = size; j > i; j--) {
      printf(" ");
    }
    // printing number
    for (int k = 0; k < i * 2 - 1; k++) {
      if (k == 0 || k == 2 * i - 2) {
        printf("%d", num++);
      } else {
        printf(" ");
      }
    }
    // set the number to 1
    num = 1;
    printf("\n");
  }
  // downside triangle
  for (int i = 1; i < size; i++) {
    // printing spaces
    for (int j = 0; j < i; j++) {
      printf(" ");
    }
    // printing number
    for (int k = (size - i) * 2 - 1; k >= 1; k--) {
      if (k == 1 || k == (size - i) * 2 - 1) {
        printf("%d", num++);
      } else {
        printf(" ");
      }
    }
    // set the number to 1
    num = 1;
    printf("\n");
  }
  return 0;
}

Output:

    1
   1 2
  1   2
 1     2
1       2
 1     2
  1   2
   1 2
    1

11. HourGlass Number Pattern program in C

Hourglass number pattern is a pattern with a shape of an hourglass. It is made up of 2 parts: upside pyramid, and downside pyramid.

123456789
 1234567
  12345
   123
    1
   123
  12345
 1234567
123456789

Here is the complete code to create hourglass number pattern in C:

#include <stdio.h>

int main() {
  int size = 5;
  // reversed pyramid number pattern
  for (int i = 0; i < size; i++) {
    // printing spaces
    for (int j = 0; j < i; j++) {
      printf(" ");
    }
    // printing number
    for (int k = 0; k < (size - i) * 2 - 1; k++) {
      printf("%d", k + 1);
    }
    printf("\n");
  }
  // pyramid number pattern
  for (int i = 2; i <= size; i++) {
    // printing spaces
    for (int j = size; j > i; j--) {
      printf(" ");
    }
    // printing number
    for (int k = 0; k < i * 2 - 1; k++) {
      printf("%d", k + 1);
    }
    printf("\n");
  }
  return 0;
}

Output:

123456789
 1234567
  12345
   123
    1
   123
  12345
 1234567
123456789

12. Pascal Triangle Pattern program

Pascal triangle number pattern is a triangle pattern with numbers. It is made up of 2 parts: upside pyramid, and downside pyramid.

1
12
123
1234
12345
1234
123
12
1

Here is the complete code to create pascal triangle number pattern in C:

#include <stdio.h>

int main() {
  // left pasal triangle
  int size = 5;

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

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

Output:

1
12
123
1234
12345
1234
123
12
1

13. Heart Number Pattern program

Heart number pattern is a very complex pattern to create. Generally it breaks for bigger number because of multi digit numbers.

 12   12
1234 1234
123456789
 1234567
  12345
   123
    1

Here is the complete code to create heart number pattern in C:

#include <stdio.h>

int main() {
  int rows = 5, num; // number of rows in the pattern

  // upper half of the heart
  for (int i = rows / 2; i <= rows; i += 2) {
    num = 1;
    for (int j = 1; j < rows - i; j += 2) {
      printf(" "); // print spaces for empty positions
    }
    for (int j = 1; j <= i; j++) {
      printf("%d", num++); // print numbers for each position in the heart
    }
    num = 1;
    for (int j = 1; j <= rows - i; j++) {
      printf(" "); // print spaces for empty positions
    }
    for (int j = 1; j <= i; j++) {
      printf("%d", num++); // print numbers for each position in the heart
    }
    printf("\n"); // move to the next line for the next row
  }

  // lower half of the heart
  for (int i = rows; i >= 1; i--) {
    for (int j = i; j < rows; j++) {
      printf(" "); // print spaces for empty positions
    }
    num = 1;
    for (int j = 1; j <= (i * 2) - 1; j++) {
      printf("%d", num++); // print numbers for each position in the heart
    }
    printf("\n"); // move to the next line for the next row
  }
  return 0;
}

Output:

 12   12
1234 1234
123456789
 1234567
  12345
   123
    1

14. Plus Number Pattern program

Plus number pattern is a pattern of shape of plus sign (+).

  1  
  1  
12345
  1  
  1

Here is the complete code to create plus number pattern in C:

#include <stdio.h>

int main() {
  // size of plus, use odd number
  int size = 5;
  int num = 1;

  for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
      // print only numbers in middle row
      if (i == size / 2) {
        printf("%d",num++);
      }
      // other than middle row, print numbers only at index size/2
      else {
        num = 1;
        if (j == size / 2) {
          printf("%d", num);
        } else {
          printf(" ");
        }
      }
    }
    printf("\n");
  }
  return 0;
}

Output:

  1  
  1  
12345
  1  
  1

15. Cross Number Pattern program

Cross number pattern is a pattern of shape of cross sign (×).

1   1
 2 2 
  3  
 4 4 
5   5

Here is the complete code to create cross number pattern in C:

#include <stdio.h>

int main() {
  // size of cross, use odd number
  int size = 5;

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

Output:

1   1
 2 2 
  3  
 4 4 
5   5

Conclusion

So, these are the most common number pattern programs in C.

Complete code of all the pattern is given. You can modify the code to create different patterns.

Happy Coding!😇