Count number of steps to reduce a number to zero (subtract last digit) Dry Run in C

Count number of steps to reduce a number to zero (subtract last digit) is an interactive C dry run visualizer from the For 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.

Count number of steps to reduce a number to zero (subtract last digit) Program Code

#include <stdio.h>
int main() {
    int num, steps = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    
    int temp = num;
    for(; temp != 0; ) {
        int digit = temp % 10;
        if(digit != 0) {
            temp = temp - digit;
            steps++;
        } else {
            // If digit is 0, subtract 1 or handle specially
            temp = temp - 1;
            steps++;
        }
    }
    
    printf("Steps to reduce %d to zero: %d\n", num, steps);
    return 0;
}

View the complete For programs page.

Program Console Count number of steps to reduce a number to zero (subtract last digit) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.