Find numbers equal to sum of proper divisors (for loop) Dry Run in C

Find numbers equal to sum of proper divisors (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 numbers equal to sum of proper divisors (for loop) Program Code

#include <stdio.h>
int sumOfDivisors(int n) {
    int sum = 0, i;
    for(i = 1; i <= n/2; i++) {
        if(n % i == 0) sum += i;
    }
    return sum;
}

int main() {
    int limit, num, i, j;
    printf("Enter limit: ");
    scanf("%d", &limit);
    
    printf("Numbers where sum of proper divisors equals the number (Perfect Numbers):\n");
    int found = 0;
    
    for(num = 2; num <= limit; num++) {
        int sum = sumOfDivisors(num);
        
        if(sum == num) {
            printf("%d = ", num);
            // Print the divisors
            for(i = 1; i <= num/2; i++) {
                if(num % i == 0) {
                    printf("%d", i);
                    int next = i + 1;
                    int hasMore = 0;
                    for(j = next; j <= num/2; j++) {
                        if(num % j == 0) { hasMore = 1; break; }
                    }
                    if(hasMore) printf(" + ");
                }
            }
            printf("\n");
            found++;
        }
    }
    
    if(found == 0) printf("No perfect numbers found up to %d\n", limit);
    else printf("\nTotal perfect numbers found: %d\n", found);
    
    return 0;
}

View the complete Nested programs page.

Program Console Find numbers equal to sum of proper divisors (for loop) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.