I am new to C and am having trouble with arrays. The code below produces strange output specifically at a[4].
Code:
#include <stdio.h>
int main(void)
{
int num;
int i = 0;
int factor = 2;
int a[1024];
printf("Enter a number: ");
scanf("%d", &num);
while (num != factor) {
if (num % factor == 0) {
num = num / factor;
a[i] = factor;
i++;
}
else {
factor++;
i++;
}
}
a[i] = factor;
int j = 0;
while (j <= i) {
printf("%d\n", a[j]);
j++;
}
return 0;
}
Output
Code:
Enter a number: 144
2
2
2
2
32767
3
3
Thanks in advance for your help.