C Number Combinations Program

Beginner
6 min read
Updated: Oct 2026
3 programs
Live preview

What Are Number Combinations?

In coding interviews, number combinations usually means listing selections from a set with nested loops. If order matters, you are listing permutations. If order is ignored, you are listing combinations (each subset once).

Remember
Permutation: order matters
  [1, 2, 3] → 6 lines (3! orderings)

Combination: order ignored
  choose 2 from [10, 20, 30, 40] → 6 pairs
  choose 3 from [10, 20, 30, 40] → 4 triples
PatternIndex ruleCount
3 permutationsi != j != k (distinct slots)3! = 6
Unordered pairsj = i + 1C(n, 2)
Unordered triplesi < j < kC(n, 3)

How to Generate Combinations in C

Four clear steps — then pick permutations or increasing-index combinations.

  1. Store the numbers in an array.
  2. Decide whether order matters (permutations) or not (combinations).
  3. Use nested loops: distinct indices for orderings, or j = i + 1 / k = j + 1 for subsets.
  4. Print each valid selection once.
MethodIdeaTimeBest for
3 nested loopsAll orderings with distinct indicesO(1) for n = 3Small fixed-size permutations
Pairs j = i + 1Choose 2, order ignoredO(n²)Unordered pair listing
Triples i < j < kChoose 3, order ignoredO(n³)Unordered triple listing

Pseudocode

Pseudocode
function PrintThreePermutations(arr):  // length 3
    for i from 0 to 2:
        for j from 0 to 2:
            for k from 0 to 2:
                if i, j, k are all different:
                    output arr[i], arr[j], arr[k]

function PrintUnorderedPairs(arr):
    n = length(arr)
    for i from 0 to n - 1:
        for j from i + 1 to n - 1:
            output arr[i], arr[j]

function PrintUnorderedTriples(arr):
    n = length(arr)
    for i from 0 to n - 1:
        for j from i + 1 to n - 1:
            for k from j + 1 to n - 1:
                output arr[i], arr[j], arr[k]

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
PermutationOrder matters — distinct indices
CombinationOrder ignored — increasing indices
Pairs trickfor (j = i + 1; ...)
Triples tricki < j < k via nested starts
Count (3 distinct)3! = 6 orderings
Count (pairs)C(n, 2) = n(n-1)/2
Classic check[1,2,3] → 6 lines; 4 values → 6 pairs

Live Preview

Enter three integers to list all distinct-index orderings (permutations).

Use commas or spaces. Needs exactly three whole numbers.

Result
Input: 1, 2, 3
Orderings (6):
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Worked Walkthrough — [1, 2, 3]

Trace the permutation loops, then compare with unordered pairs from four values.

Calculation
Permutations of [1, 2, 3]
  i,j,k pick three different slots
  Valid lines: 1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, 3 2 1
  Count = 3! = 6

Unordered pairs from [10, 20, 30, 40]
  (i, j) with j > i
  10 20, 10 30, 10 40, 20 30, 20 40, 30 40
  Count = C(4, 2) = 6

C Programs

Start with all orderings of three numbers, then list unordered pairs and triples with increasing indices.

Example 1 — All Orderings of Three Numbers

Order matters. Use three loops and require distinct indices.

C
#include <stdio.h>

void print_three_permutations(int arr[])
{
    int i, j, k;

    printf("All orderings (permutations):\n");
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            for (k = 0; k < 3; k++)
            {
                if (i != j && j != k && i != k)
                {
                    printf("%d %d %d\n", arr[i], arr[j], arr[k]);
                }
            }
        }
    }
}

int main(void)
{
    int arr[] = { 1, 2, 3 };
    print_three_permutations(arr);
    return 0;
}

How It Works

  1. Each loop picks one position in the printed triple.
  2. The guard i != j && j != k && i != k blocks reusing the same array slot.
  3. For three distinct values you always get 3! = 6 lines.
  4. This is a permutation listing — 1 2 3 and 2 1 3 both appear.
  5. The function assumes the array length is exactly 3.

Example 2 — Unordered Pairs

Order does not matter. Start the inner loop at i + 1.

C
#include <stdio.h>

void print_unordered_pairs(int arr[], int n)
{
    int i, j;

    printf("Unordered pairs (choose 2 from %d):\n", n);
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            printf("%d %d\n", arr[i], arr[j]);
        }
    }
}

int main(void)
{
    int arr[] = { 10, 20, 30, 40 };
    print_unordered_pairs(arr, 4);
    return 0;
}

How It Works

  1. Outer loop picks the first index i.
  2. Inner loop starts at i + 1, so every pair uses increasing indices.
  3. Reverse duplicates like 20 10 never appear.
  4. For four values you get C(4, 2) = 6 pairs.
  5. This is the classic “choose 2” nested-loop pattern.

Example 3 — Unordered Triples

Extend the pair idea to three loops with i < j < k.

C
#include <stdio.h>

void print_unordered_triples(int arr[], int n)
{
    int i, j, k;

    printf("Unordered triples (choose 3 from %d):\n", n);
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            for (k = j + 1; k < n; k++)
            {
                printf("%d %d %d\n", arr[i], arr[j], arr[k]);
            }
        }
    }
}

int main(void)
{
    int arr[] = { 10, 20, 30, 40 };
    print_unordered_triples(arr, 4);
    return 0;
}

How It Works

  1. Three nested loops pick indices in increasing order.
  2. j starts at i + 1 and k starts at j + 1.
  3. Each subset of size 3 is printed exactly once.
  4. For four values you get C(4, 3) = 4 triples.
  5. Same idea scales to choose r with r nested loops (until recursion is clearer).

Edge Cases & Pitfalls

Check these before calling the solution done.

Length

Example 1 needs length 3

The fixed triple loops assume exactly three elements. Guard the length in real code.

Duplicates

Same values, different slots

Distinct indices can still print identical-looking lines when values repeat (e.g. 2 2 3).

Order

Do not mix the rules

Using i != j for pairs still prints reverse orderings. Use j = i + 1 for combinations.

Small n

Not enough elements

If n < 2, pairs print nothing. If n < 3, triples print nothing — that is correct.

Time and Space Complexity

ProgramTimeExtra space
Three permutations (n = 3)O(1)O(1)
Unordered pairsO(n²)O(1)
Unordered triplesO(n³)O(1)

Output size matches the math counts: C(n, 2) pairs and C(n, 3) triples.

Key Takeaways

  • Permutation: order matters — use distinct indices.
  • Combination: order ignored — use j = i + 1.
  • Counts: 3! = 6 orderings; C(4, 2) = 6 pairs; C(4, 3) = 4 triples.
  • Interview tip: say whether order matters before coding the loops.

One line: distinct indices for orderings; increasing indices for unordered subsets.

Frequently Asked Questions

This page shows two nested-loop patterns: all orderings of three numbers (permutations), and unordered pairs or triples (combinations where order does not matter).
In a permutation, order matters (1 2 3 and 2 1 3 are different). In a combination, order is ignored, so each subset appears once.
It forces increasing indices, so you never print both (10, 20) and (20, 10). That is choose 2 from n.
3! = 6. The triple nested loops with distinct indices print all of them.
Listing pairs is O(n²). Listing unordered triples is O(n³). Fixed-size three-element permutations are O(1).

Next: Odd Number

Check whether an integer has a nonzero remainder by 2.

Odd number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful