R28 Convert recursion to iteration Dry Run in C

R28 Convert recursion to iteration 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.

R28 Convert recursion to iteration Program Code

#include <stdio.h>

// Recursive version (exponential time)
int fibRecursive(int n) {
    if(n <= 1) {
        return n;
    }

    int first = fibRecursive(n-1);
    int second = fibRecursive(n-2);
    int answer = first + second;
    return answer;
}

// Iterative version (linear time, constant space)
int fibIterative(int n) {
    if(n <= 1) return n;
    int prev2 = 0, prev1 = 1, current;
    for(int i = 2; i <= n; i++) {
        current = prev1 + prev2;
        prev2 = prev1;
        prev1 = current;
    }
    return prev1;
}

int main() {
    int n = 5;
    printf("Recursive fib(%d) = %d\n", n, fibRecursive(n));
    printf("Iterative fib(%d) = %d\n", n, fibIterative(n));
    return 0;
}

View the complete Recursion programs page.

Program Console R28 Convert recursion to iteration Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.