Home > Back-end >  2 4 6 8 10 12 14 16... 100 sum algorithm where is wrong
2 4 6 8 10 12 14 16... 100 sum algorithm where is wrong

Time:09-20

2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 +... + 100 sum
Int o, p;
O=0, p=1;
While (p<=50)
{
O=o + 2 * p;
P=p++;
}
Printf (" \ n o=% d ", o);//n output value is 2550

2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 +... + 100 sum
Int h, j, z;
H=0, j=0, z=1;
While (z<=50)
{
H=h + j;
J * z=2;
Z=z++;
}
Printf (" \ n h=% d ", h);//n output value is 2450 , where is wrong

CodePudding user response:

P=p++; What is?
P++;
Or
++p;
Or
P=p + 1;

CodePudding user response:

This topic with the for statement is simple:
 # include & lt; Stdio. H> 
Int main ()
{
Int sum=0, I;
for(i=2; i<=100; I +=2)
The sum +=I;
Printf (" sum=% d \ n ", sum);
return 0;
}

CodePudding user response:

While (z<=50)
{
J * z=2;
H=h + j;
Z++;
}

CodePudding user response:

reference u010165006 reply: 3/f
while (z<=50)
{
J * z=2;
H=h + j;
Z++;
}




The order

CodePudding user response:

To perform j z=2 * and perform h=h + j; Execution order is very important,
  • Related