#Sum of even numbers between two inputs, for example X & Y

29 messages · Page 1 of 1 (latest)

rapid prism
#

How do I print out the last line?

storm shuttleBOT
#

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.

rapid prism
#
#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

rapid prism
#

it works

#

I just don't know how to do the last line

#

that says

#

"Explanation of sample: ..."

silk imp
#

to do that you can't modify X

#

wait no

#

ok

#

so you need to print out what you added

rapid prism
#

yep

silk imp
#

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

rapid prism
#

oh dam

#

damn

#

it worked

#

thank you so much

silk imp
#

np

storm shuttleBOT
#

@rapid prism Has your question been resolved? If so, run !solved :)

rapid prism
#

!solved