Magic Number Pattern Dry Run in C

Magic Number Pattern is an interactive C dry run visualizer from the Patterns 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.

Magic Number Pattern Program Code

#include <stdio.h>
int main() {
    int n = 3, i = 0, j = 1, sq[3][3] = {0}, val, r, c;
    for(val = 1; val <= 9; val++) {
        sq[i][j] = val;
        int ni = (i - 1 + 3) % 3;
        int nj = (j + 1) % 3;
        if(sq[ni][nj] != 0) {
            i = (i + 1) % 3;
        } else {
            i = ni; j = nj;
        }
    }
    for(r = 0; r < 3; r++) {
        for(c = 0; c < 3; c++) printf("%d ", sq[r][c]);
        printf("\n");
    }
    return 0;
}

View the complete Patterns programs page.

Program Console Magic Number Pattern Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.