5 Replies - 199 Views - Last Post: 19 January 2012 - 05:00 PM Rate Topic: -----

#1 bennyb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

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  Icon User is offline

  • New D.I.C Head

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

Re: Help with counting even numbers in different ranges

Posted 19 January 2012 - 01:22 PM

View PostCasiOo, 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  Icon User is offline

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

Reputation: 8030
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

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 :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1