> I don't understand how to write a C Program to find the sum of the even integers between X and Y where X and Y are p

I don't understand how to write a C Program to find the sum of the even integers between X and Y where X and Y are p

Posted at: 2014-12-18 
First use scanf to read x and y.

If x is greater than y swap them.

Now do a for loop that goes from x to y.

Inside that loop have an if statement that checks if i is even.

If i is even add that to an accumulator.

Use printf to show the value of the accumulator.

Don't forget to make sure that the accumulator starts with 0.

#include

int main(){

int x, y, sum = 0, i, t;

scanf("%d %d", &x, &y);

if(x > y){

t = x;

x = y;

y = t;

}

for(i = x; i <= y; ++i){

if(i % 2 == 0){

sum += i;

}

}

printf("%d\n", sum);

return 0;

}

I suggest you solve the same problem by trial and error first, and then try to code it. Start with x=11 and y=20 as an example. The first even x number will be 12. So you want 12+14, 12+16, 12+18, and you don't want 12+20. The next even x number will be 14. So you want 14+16, 14+18, and you don't want 14+20. And so on.

Create a loop that counts from X/2 to Y/2

add the loop count * 2 to your accumulator variable.

C is a mighty cryptic language for kids, BASIC was first written specifically as a language for learning programming concepts with more English like terms.

I was teaching my 7th grade class how computers can be programmed to do just about anything, and this is the example that I provided them with in class. However, they wanted to see an actual program run and as a math teacher, my programming skills are not very good. How would I go about writing a program that would take two integers from the user, compare them to see if they are ever or odd and if one is less than the other, and then calculate the sum of all even integers between those two numbers? I would prefer if any code or examples were in the C programming language, since this is probably the language I know the most about. Thanks!