#Sum of even numbers between two inputs, for example X & Y
29 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
#include <stdio.h>
int main()
{
int X, Y, sum = 0;
printf("Enter two integer values: ");
scanf("%d%d", &X, &Y);
printf("Sum of all even numbers between %d & %d: ", X, Y);
while (X <= Y)
{
if (X%2 == 0)
{
sum = sum + X;
}
X++;
}
printf("%d\n", sum);
return 0;
}
with this code I can output the sum of even numbers between two inputs
whats not working?
it works
I just don't know how to do the last line
that says
"Explanation of sample: ..."
yep
so add a printf inside the if (X%2 == 0)
like
if (X%2 == 0)
{
printf("%d + ", X);
sum = sum + X;
}
and at the end
you change printf("%d\n", sum);
into printf("\b\b= %d\n", sum);
\b is a escape sequence to delete a char
np
@rapid prism Has your question been resolved? If so, run !solved :)
!solved