How to sum the proper divisors of a number in C

1 Answer

0 votes
#include <stdio.h>

unsigned SumNumberProperDivisors(const unsigned num) {
    unsigned sum = 0;
 
    for (unsigned i = 1, j; i <= num / 2; i++) {
        if (num % i == 0) {
            printf("%u, ", i);
            sum += i;
        }
    }
    return sum;
}
 
int main()
{
    printf("\n%u", SumNumberProperDivisors(220));
 
    return 0;
}
 
 
 
/*
run:
 
1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, 
284
 
*/

 

 



answered Oct 22, 2022 by avibootz
edited Nov 14, 2023 by avibootz
...