R27 Tail recursion vs non-tail recursion Dry Run in C

R27 Tail recursion vs non-tail 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.

R27 Tail recursion vs non-tail recursion Program Code

#include <stdio.h>

// NON-TAIL RECURSION: operation AFTER recursive call
int factNonTail(int n) {
    if(n <= 1) {
        return 1;
    }
    int smallerResult = factNonTail(n-1);
    int result = n * smallerResult;  // Multiply AFTER return
    return result;
}

// TAIL RECURSION: recursive call is LAST operation
int factTail(int n, int accumulator) {
    if(n <= 1) {
        return accumulator;
    }
    int newAccumulator = n * accumulator;
    return factTail(n-1, newAccumulator);  // No work after return
}

int main() {
    int n = 5;
    printf("Non-tail factorial: %d\n", factNonTail(n));
    printf("Tail recursive factorial: %d\n", factTail(n, 1));
    return 0;
}

View the complete Recursion programs page.

Program Console R27 Tail recursion vs non-tail recursion Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.