lundi 16 décembre 2019

indexNumber in for-loop does not increase

I get the idea of if the if condition is wrong the if case won't be executed, but the for loop will still continue. But somehow I'm stucked upon this simple problem.

int divisorA[20]={0},indexA=0,a=42;

    for (int i =1;i<a+1;++i){
        if (a % i == 0){
            divisorA[indexA] = i;
            ++indexA;
            printf("%d %d\n",divisorA[indexA],indexA);
        }

   }

Output

0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8

Desired

1 1
2 2
3 3
6 4
7 5
14 6
21 7
42 8

Aucun commentaire:

Enregistrer un commentaire