-1

How can I display and add all even numbers? The current code displays numbers between 2 numbers in an ascending manner.

#include <stdio.h>

main() {
  int a;
  int b;

  printf("Enter integer a:");
  scanf("%d", &a);

  printf("Enter integer b:");
  scanf("%d", &b);

 if(b > a)
  {
      do {
        printf("Result: %d\n", b);
        b--;
      } while (a <= b);
  }
  else
  {
      do {
        printf("Result: %d\n", a);
        a--;
      } while (a >= b);
  }
  
  }
user438383
  • 5,716
  • 8
  • 28
  • 43

3 Answers3

2

To check if an integer is even you can check if the least significant bit is zero.

To check if an integer is odd you can check if the least significant bit is one.

You can do that using bitwise AND (&).

Something like:

if(b > a)
{
    if (b & 1) b--;  // Make b even
    if (a & 1) a++;  // Make a even
    int sum = 0;
    do 
    {
        sum += b;
        printf("b is %d, sum is %d\n", b, sum);
        b = b - 2;
      } while (b >= a);
  }
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • ok ok I'm done editing :p – Cid Nov 11 '21 at 08:50
  • 1
    @Cid Nice edit but we just edited at the same time :-) – Support Ukraine Nov 11 '21 at 08:50
  • my problem right now is how to display the the first output (the first part of the code) and even numbers –  Nov 11 '21 at 08:53
  • 1
    For "old" programmers like us, `a & 1`, immediately says "odd number", but I think `(a % 2) != 0` is clearer and any modern compiler will most likely recognize the case and generate the same code. Personally, I am going away from many of these tricks to nudge the compiler since the compiler will likely do the optimization and I can just focus on clarity. I have to admit, though, that a part of me loves the old tricks :-) – nielsen Nov 11 '21 at 09:17
  • @Monsterbeginner I don't understand the follow up question in your comment – Support Ukraine Nov 11 '21 at 10:34
  • @nielsen `For "old" programmers like us, ....` hmm... Do we know each other? Your nick seems very familiar ;-) – Support Ukraine Nov 11 '21 at 11:44
  • @4386427 Haha, probably not, I was making a bold assumption, sorry if I stepped on a young toe :-) – nielsen Nov 11 '21 at 11:51
1

All even numbers are divisible by 2. You need to check if the remainder of division by 2 is equal to zero. In order to do it, you can use the modulo operator (%). To display only even numbers:

if ((b%2) == 0) {
  printf("Result: %d\n", b);
}
giusepped
  • 122
  • 3
  • 13
0

I'd write a function which I could test for different possible combinations of the extremes:

#include <stdio.h>

void evens(int a, int b)
{
    // Make sure to always start from the greatest value.
    if ( a > b ) {
        int tmp = a;
        a = b;
        b = tmp;
    }

    // Make sure to always start from an EVEN value.
    if ( b % 2 != 0 ) { 
        --b; 
    }

    int sum = 0;
    while ( a <= b ) {
        printf("%d ", b);
        sum += b;
        b -= 2;    // Jump directly to the previous even number.
    }
    printf("\nSum: %d\n", sum);
}

int main(void)
{
    // Those should all print "10 8 6 4 2 \nSum: 30\n"  
    evens(1, 10);
    evens(10, 1);
    evens(2, 11);
    evens(11, 1);
    evens(2, 10);
}
Bob__
  • 12,361
  • 3
  • 28
  • 42