I'm learning C programming online and I wrote this code to find the perfect numbers in a given range, but I'd like to show the result with the sum of the divisors, for example:
1 + 2 + 3 = 6 PERFECT !!!
1 + 2 + 4 + 7 + 14 = 28 PERFECT !!!
My code looks like this:
#include <stdio.h>
int main(int argc, const char * argv[]) {
int first_interval;
int last_interval;
int number;
int divider;
int sum = 0;
printf ("Enter first and last interval's number\n");
scanf("%d %d", &first_interval, &last_interval);
for (number = first_interval; number >= first_interval && number <= last_interval; number++ ) {
for (divider = 1; divider < number; divider++) {
if (number % divider == 0) {
sum = sum + divider;
}
}
if (sum == number) {
printf("%d = %d PERFECT !!!\n", number, sum);
}
sum = 0;
}
return 0;
}
How can I get the desired result, does anyone have any ideas?