C Example Code2026-03-252 min read
Master number patterns in C including Floyd's triangle, continuous number pyramids, and binary alternating grids with runnable C programs.
What are Number Patterns?
Number pattern programs in C require mathematical indexing to compute numbers based on current row index i and column index j.
1. Floyd's Triangle in C
Floyd's triangle prints consecutive natural numbers in a right-angled triangular format:
c (ISO Standard)#include <stdio.h> void printFloydsTriangle(int rows) { int count = 1; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf("%3d ", count++); } printf("\n"); } }
2. Binary Alternating Triangle (0 and 1)
c (ISO Standard)#include <stdio.h> void printBinaryTriangle(int rows) { for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { /* If sum of indices (i+j) is even, print 1; else 0 */ printf("%d ", (i + j) % 2 == 0 ? 1 : 0); } printf("\n"); } } int main() { int rows = 5; printf("Floyd's Triangle (%d rows):\n", rows); printFloydsTriangle(rows); printf("\nBinary Alternating Triangle (%d rows):\n", rows); printBinaryTriangle(rows); return 0; }
Sample Output
text (ISO Standard)Floyd's Triangle (5 rows): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Binary Alternating Triangle (5 rows): 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
Complexity Analysis
- Time Complexity:
O(n²)for generating $n(n+1)/2$ elements. - Space Complexity:
O(1)constant auxiliary storage.
Related Guides
- Review star geometries in Star Pattern Programs in C.
- Master 2D nested loops in Arrays in C Programming.