Home > Back-end >  And even within 100 and why the multiple values, but the end result is that right
And even within 100 and why the multiple values, but the end result is that right

Time:10-12

#include

Void main ()

{
int sum=0;
int i;
For (I=1; I & lt;=100; I++)
{if (I % 2==0)
{
Sum=sum + I;
}
Printf (" % d \ n ", sum);

}
}

CodePudding user response:

 for (I=1; I & lt;=100; I++) 
{if (I % 2==0)
{
Sum=sum + I;
}
Printf (" % d \ n ", sum);

}

Have a look at the code, your printf is inside the for loop, that is to say, every even number add you output the sum again
If just want to be the final result, placed the printf in a layer of outside is ok!
 # include & lt; stdio.h> 

Void main ()

{
int sum=0;
int i;
For (I=1; I & lt;=100; I++)
{if (I % 2==0)
{
Sum=sum + I;
}
}
Printf (" % d \ n ", sum);
}

CodePudding user response:

You put the
Printf (" % d \ n ", sum);
Take out
On the outside of the for loop try
  • Related