Number Patterns in C Programming — PBA Institute Tutorial
Chapter 08 · C Programming Series
10 min read Beginner

Number Patterns in C

In C programming, a "number pattern" typically refers to the practice of arranging numbers in a structured format using nested loops. These patterns can be simple or complex, depending on the logic applied.

What is number pattern ?

Basic Example

C Code
#include <stdio.h>
 main() {
    int i, j;
    for (i = 1; i <= 5; i++) 
	{       
        for (j = 1; j <= i; j++) 
		{   
            printf("%d", j);         
        }
        printf("\n");                
    }
    return 0;
}
Output
1
12
123
1234
12345

Explanation

  • Outer Loop (i) : Runs from 1 to 5, representing each row.
  • Inner Loop (j) : Runs from 1 to the current value of i, representing the numbers in each row.
  • Print Statement : Prints the current value of j.
  • New Line : After the inner loop completes, a new line is printed to move to the next row.

More examples

C Code
#include <stdio.h>
main() {
    int i, j;
    for (i = 5; i >= 1; i--) {       
        for (j = 1; j <= i; j++) {   
            printf("%d", j);         
        }
        printf("\n");                
    }
}
Output
12345
1234
123
12
1
C Code
#include <stdio.h>
main() {
    int i, j, k, n = 5;
    for (i = 1; i <= n; i++) {
        for (j = i; j < n; j++) {
            printf(" "); 
        }
        for (k = 1; k < i; k++) {
            printf("%d", k); 
        }
        for (k = i; k >= 1; k--) {
            printf("%d", k); 
        }
        printf("\n");
    }
}
Output
        1
      121
    12321
  1234321
123454321
C Code
#include<stdio.h>
main()
{
	int i,j;
	for(i=5;i>=1;i--)
	{
		for(j=i;j>=1;j--)
		{
			printf("%d",j);
		}
		printf("\n");
	}
}
Output
54321
4321
321
21
1
C Code
#include<stdio.h>
main()
{
	int i,j;
	for(j=5;j>=1;j--)
	{
		for(i=j;i<=5;i++)
		{
			printf("%d",i);
		}
		printf("\n");
	}
}
Output
5
45
345
2345
12345

Concepts Involved

  • Loops : for, while, or do-while loops are used to repeat code.
  • Nested Loops : Using loops inside other loops to create complex patterns.
  • Conditional Statements : Sometimes if-else statements are used for more intricate patterns.
  • Print Formatting : Proper use of printf for aligning and formatting output.

Conclusion

Number patterns in C are a great way to practice and understand loops and nested loops. They help in developing logic building and understanding the control flow in programs. Once you get comfortable with basic patterns, you can experiment with more complex ones.