Find amicable numbers in a range (for loop) Dry Run in C

Find amicable numbers in a range (for loop) is an interactive C dry run visualizer from the Nested 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.

Find amicable numbers in a range (for loop) Program Code

#include <stdio.h>
int main() {
    int start, end, a, i;
    printf("Enter range (start end): ");
    scanf("%d %d", &start, &end);
    
    printf("Amicable pairs (a,b) where %d <= a < b <= %d:\n", start, end);
    int found = 0;
    for(a = start; a <= end; a++) {
        // Sum proper divisors of a
        int sum_a = 0;
        for(i = 1; i <= a/2; i++) {
            if(a % i == 0) sum_a += i;
        }
        
        int b = sum_a;
        if(b > a && b <= end) {
            // Sum proper divisors of b
            int sum_b = 0;
            for(i = 1; i <= b/2; i++) {
                if(b % i == 0) sum_b += i;
            }
            if(sum_b == a) {
                printf("(%d, %d)\n", a, b);
                found++;
            }
        }
    }
    printf("Total amicable pairs: %d\n", found);
    return 0;
}

View the complete Nested programs page.

Program Console Find amicable numbers in a range (for loop) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.