Replace all 0's with 1's (Recursion) Dry Run in C

Replace all 0's with 1's (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.

Replace all 0's with 1's (Recursion) Program Code

#include <stdio.h>

int replaceZerosWithOnes(int n) {
    if(n == 0) {
        return 1;  // If original number was 0, replace with 1
    }
    if(n < 10) {
        return (n == 0) ? 1 : n;
    }
    int lastDigit = n % 10;
    if(lastDigit == 0) {
        lastDigit = 1;
    }
    return replaceZerosWithOnes(n / 10) * 10 + lastDigit;
}

int main() {
    int num = 1020450;
    int result = replaceZerosWithOnes(num);
    printf("Original: %d\n", num);
    printf("After replacing 0 with 1: %d\n", result);
    return 0;
}

View the complete Recursion programs page.

Program Console Replace all 0's with 1's (Recursion) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.