# Help with counting even numbers in different ranges

Page 1 of 1

## 5 Replies - 199 Views - Last Post: 19 January 2012 - 05:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=263484&amp;s=76ba1195f459793d422575bd34c50c96&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bennyb

• New D.I.C Head

Reputation: 0
• Posts: 31
• Joined: 17-December 11

# Help with counting even numbers in different ranges

Posted 19 January 2012 - 08:08 AM

I am trying to create a program to add the number of even numbers up between two numbers inputted by a user - the program works fine as long as the first number entered is smaller than the second. For example entering 1 and 6 would print 12 (this works) but when entering 6 and 1 it prints 0 (should be 12).

I know this code is what is causing the problem but i don't know how to add the fact number 1 maybe bigger then number 2

```for (int j = number1; j <= number2; j++) {
if (j % 2 ==0) {
```

Thanks for your help in advance.

Is This A Good Question/Topic? 0

## Replies To: Help with counting even numbers in different ranges

### #2 Dogstopper

• The Ninjaducky

Reputation: 2696
• Posts: 10,556
• Joined: 15-July 08

## Re: Help with counting even numbers in different ranges

Posted 19 January 2012 - 08:14 AM

Just add an if statement before the loop. If number1 > number, then switch the values of 1 and 2. Then your loop should work. As it is, it only loops WHILE num1 >= num2.
Was This Post Helpful? 0

### #3 CasiOo

• D.I.C Lover

Reputation: 996
• Posts: 2,212
• Joined: 05-April 11

## Re: Help with counting even numbers in different ranges

Posted 19 January 2012 - 08:16 AM

Well you just have to check which of the two numbers are the highest/smallest

```public int evenNumbers(int number1, int number2) {
int smallest = Math.min(number1, number2);
int highest = Math.max(number1, number2);

for (int j = smallest; j <= highest; j++)
if (j% 2 == 0)
...
}

```

Was This Post Helpful? 1

### #4 bennyb

• New D.I.C Head

Reputation: 0
• Posts: 31
• Joined: 17-December 11

## Re: Help with counting even numbers in different ranges

Posted 19 January 2012 - 01:22 PM

CasiOo, on 19 January 2012 - 08:16 AM, said:

Well you just have to check which of the two numbers are the highest/smallest

```public int evenNumbers(int number1, int number2) {
int smallest = Math.min(number1, number2);
int highest = Math.max(number1, number2);

for (int j = smallest; j <= highest; j++)
if (j% 2 == 0)
...
}

```

Thanks a lot, i know what to do now - appreciate your time!
Was This Post Helpful? 0

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8030
• Posts: 31,176
• Joined: 06-March 08

## Re: Help with counting even numbers in different ranges

Posted 19 January 2012 - 04:46 PM

No need to test all elements in the loop, just the smallest once before starting

```public int evenNumbers(int number1, int number2) {
int smallest = Math.min(number1, number2);
int highest = Math.max(number1, number2);

if(smallest % 2 != 0)
++smallest;

for (int j = smallest; j <= highest; j += 2)
...
}

```

Happy coding
Was This Post Helpful? 1

### #6 CasiOo

• D.I.C Lover

Reputation: 996
• Posts: 2,212
• Joined: 05-April 11

## Re: Help with counting even numbers in different ranges

Posted 19 January 2012 - 05:00 PM

Ah yes didn't think about that. That is actually a great one pbl
Was This Post Helpful? 0

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).