I am trying to write a program that prints the even divisors of whatever number is entered by the user. For some reason, when the user enters 10 and the program prints out:
10 is evenly divisible by:
1, 3, 2, 27, logout
I have no idea where it is getting these numbers from. If I uncomment the second to last printf statement, I get the following:
10 is evenly divisible by:
i = 0
1, i = 1
2, i = 2
5, i = 3
32767, logout
Why is it doing this?
Here's my code:
#include <stdio.h>
int main(void ) {
int n, i, leng = 0, arr[leng];
printf("Enter an integer\n");
scanf("%i",&n);
printf("%i is evenly divisible by:\n", n);
for (i = 1; i <= n / 2; i++) {
if (n % i == 0) {
arr[leng] = i;
leng++;
}
}
for (i = 0; i <= leng; i++) {
printf("i = %i\n", i);
printf("%i, ", arr[i]);
}
}
leng = 0, arr[leng];