In your declaration of the array, you have undefined behaviour because you used the wrong size:
main() {
int i,
t[i],
d, /*divider*/
sum,
product;
i = 0;
printf("Enter your natural numbers. \n");
while (i <= 9) {
printf("Number %d : ", i + 1);
scanf( "%d", &t[i]);
i++;
}
You probably meant to declare
t[MAX_BOUND+1];
(MAX_BOUND
would be wrong, since you use the element t[MAX_BOUND]
).
At the point where t
is declared, i
has indeterminate value (not unlikely to be 0).
With indeterminate array size, the accesses t[i]
produce yet more indeterminate values (and again are undefined behaviour if i >= sizeof t / sizeof t[0]
).
The printing part,
if(sum == t[i])
printf("%d is a perfect number. \n", t[i]);
else
product = product * t[i];
should be moved after the loop used to determine the divisor sum. With that inside the loop, you multiply product
with t[i]
a total of t[i] - 1
times (or t[i] - 2
if one of the intermediate sums equals t[i]
) if t[i]
is not perfect, and t[i]/2-1
times if t[i]
is perfect. Also, you print t[i]/2
times for perfect numbers, and once for abundant numbers if one of the intermediate sums equals t[i]
(I'm ignoring the theoretical possibility of odd perfect numbers, if there are any, they are far too large for int
).
Doing that produces correct output here.