nCr (Binomial Coefficient) - Recursion Dry Run in C

nCr (Binomial Coefficient) - Recursion is an interactive C dry run visualizer from the Recursion programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

nCr (Binomial Coefficient) - Recursion Program Code

#include <stdio.h>

// nCr = n! / (r! * (n-r)!)
// Using Pascal's identity: nCr = (n-1)C(r-1) + (n-1)Cr
int nCr(int n, int r) {
    // Base cases
    if(r == 0 || r == n) {
        return 1;
    }
    if(r > n) {
        return 0;
    }

    // Recursive case: Pascal's rule
    int left = nCr(n - 1, r - 1);
    int right = nCr(n - 1, r);
    int result = left + right;
    return result;
}

int main() {
    int n = 5, r = 2;
    int result = nCr(n, r);
    printf("C(%d,%d) = %d\n", n, r, result);
    
    // Verify with Pascal's Triangle
    printf("Pascal's Triangle value at row %d, col %d = %d\n", n, r, result);
    return 0;
}

View the complete Recursion programs page.

Program Console nCr (Binomial Coefficient) - Recursion Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.