Kodewick
Start learning →
Starter←→0 / 50

Perfect numbers

#016Startersum += i6 min

A number is perfect if its divisors, not counting itself, add up to the number. The divisors of 6 are 1, 2, 3 (not 6), and 1 + 2 + 3 = 6. 28 = 1 + 2 + 4 + 7 + 14 is perfect too. Show all perfect numbers below 1000, smallest first, one per line.

Should show
6
28
496
main.c
#include <stdio.h>

int main(void) {
    for (int n = 2; n < 1000; n++) {
        int sum = 0;
        for (int i = 1; i <= n / 2; i++) {
            // your code here: if i is a divisor of n, add it to sum
        }
        if (sum == n) {
            printf("%d\n", n);
        }
    }
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.