Page 1 of 1

## 14 Replies - 4225 Views - Last Post: 21 November 2011 - 09:25 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=256712&amp;s=203639bfd51e9ca5feef16dbfc793855&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BaumbachR

Reputation: 0
• Posts: 13
• Joined: 20-November 11

Posted 21 November 2011 - 08:39 PM

ok i dont have my sample code on me at the moment but im wanting to write a while loop that adds all of the even numbers between two numbers the user inputs.(using JOptionPane) (1st num must be greater than 2nd num)
for an example, user inputs
numOne = 1;
numTwo = 20;

the program would then determine if first number was even if not it adds one, the sum of the even numbers between 1 and 20(i dont want it to add the equivalence of numTwo or over it i want it to stop on the even number before it)
would be 2+4+6+8+10+12+14+16+18=90. the sum would then be output to a string to be displayed in the JOptionPane.
it would determine if it was even by
if (numOne % 2 == 0)
correct?
i am just wanting some feedback code and ideas because i am having a few problems with my code(once again i dont have it with me) not adding up the right numbers.
Any help would be much appreciated

Is This A Good Question/Topic? 0

## Replies To: Adding Even numbers

### #2 fromTheSprawl

• Monomania

Reputation: 514
• Posts: 2,063
• Joined: 28-December 10

Posted 21 November 2011 - 08:43 PM

Your logic seems correct, although I could not comprehend what you mean with this:

Quote

the program would then determine if first number was even if not it adds one, the sum of the even numbers between 1 and 20(i dont want it to add the equivalence of numTwo or over it i want it to stop on the even number before it)
would be 2+4+6+8+10+12+14+16+18=90. the sum would then be output to a string to be displayed in the JOptionPane.

### #3 ChillyWilly

Reputation: 16
• Posts: 41
• Joined: 21-October 11

Posted 21 November 2011 - 08:48 PM

Your on the right track.Your if statement is correct, you put that in a loop of your choice and it should work(for,do-while,while). As for not including the last number I'm sorry you must provide your code and show that you attempted to try as per forum rules

### #4 BaumbachR

Reputation: 0
• Posts: 13
• Joined: 20-November 11

Posted 21 November 2011 - 08:50 PM

fromTheSprawl, on 21 November 2011 - 08:43 PM, said:

Your logic seems correct, although I could not comprehend what you mean with this:

Quote

the program would then determine if first number was even if not it adds one, the sum of the even numbers between 1 and 20(i dont want it to add the equivalence of numTwo or over it i want it to stop on the even number before it)
would be 2+4+6+8+10+12+14+16+18=90. the sum would then be output to a string to be displayed in the JOptionPane.

pseudocode
while numOne is less than numTwo //making sure first number is less than second if not and error is given
if numOne isnt even //making it odd
numOne++;
if numOne is even
numOne += 2; //keeps adding two until it gets to the maximum

//in this code i would like it to stop at the last even number before numTwo
all even numbers would be added to eachother as the loop completes
in this case its 2+4+6+8+10+12+14+16+18=90 and 90 would be recorded into a string to output in a JOptionPane.
understand?

This post has been edited by smohd: 21 November 2011 - 08:57 PM
Reason for edit:: code tags fixed

### #5 jon.kiparsky

• Pancakes!

Reputation: 8750
• Posts: 15,066
• Joined: 19-March 11

Posted 21 November 2011 - 08:54 PM

BaumbachR, on 21 November 2011 - 10:39 PM, said:

ok i dont have my sample code on me at the moment but im wanting to write a while loop that adds all of the even numbers between two numbers the user inputs.(using JOptionPane) (1st num must be greater than 2nd num)
for an example, user inputs
numOne = 1;
numTwo = 20;

the program would then determine if first number was even if not it adds one, the sum of the even numbers between 1 and 20(i dont want it to add the equivalence of numTwo or over it i want it to stop on the even number before it)
would be 2+4+6+8+10+12+14+16+18=90. the sum would then be output to a string to be displayed in the JOptionPane.
it would determine if it was even by
if (numOne % 2 == 0)
correct?
i am just wanting some feedback code and ideas because i am having a few problems with my code(once again i dont have it with me) not adding up the right numbers.
Any help would be much appreciated

Yes, exactly the same as your previous problem of collecting odd numbers.

### #6 fromTheSprawl

• Monomania

Reputation: 514
• Posts: 2,063
• Joined: 28-December 10

Posted 21 November 2011 - 08:57 PM

Oh I see it. Yeah you're on the right track.

Pseudothing!
input 2 numbers
while num1 < num2
if num1 less than num2, and is odd num1 adds 1
a string here appends the value of num1
else if num1 is even, add 2 to num1
a string here appends the value of num1

Take note that the appenders appear two times. Why? You need to get the 1st even value printed right? It would only do it once. So I suggest putting the check if the number is even outside the while loop since it would make the number even for once and after that it would stay even.

### #7 ChillyWilly

Reputation: 16
• Posts: 41
• Joined: 21-October 11

Posted 21 November 2011 - 08:59 PM

In that case when you set your conditional statement(if), numOne<numTwo. Making it just less than instead of less than OR EQUAL it wont execute the statement when the last number(numTwo) is reached. In your example it would get to 14+16+18 then add 2 to numOne which makes it 20, then numOne would not be less than numTwo and wont execute again. Hope this helps.

### #8 BaumbachR

Reputation: 0
• Posts: 13
• Joined: 20-November 11

Posted 21 November 2011 - 09:00 PM

ChillyWilly, on 21 November 2011 - 08:48 PM, said:

Your on the right track.Your if statement is correct, you put that in a loop of your choice and it should work(for,do-while,while). As for not including the last number I'm sorry you must provide your code and show that you attempted to try as per forum rules

I dont have my exact code on me at the moment but i will try to write a quick example
numOne = 1; //assuming user inputs one
numTwo = 20; //assuming user inputs twenty
counter = numOne; //assigns a counter
tempNum = 0; //keeps sum as loop progresses
temp = 0; //to store starting number may not use

outputStr; //to put the sum of all of the even numbers into to output

while (counter < numTwo) { //checks to make sure first is less than second
if (counter %2 == 0) { //checks for even number
tempNum = counter;
counter += 2; //adds two to even number
tempNum += counter;
} else {
counter++
}
outputStr = tempNum;
}

that isnt my exact code it was just written without an IDE

### #9 jon.kiparsky

• Pancakes!

Reputation: 8750
• Posts: 15,066
• Joined: 19-March 11

Posted 21 November 2011 - 09:02 PM

fromTheSprawl, on 21 November 2011 - 10:57 PM, said:

Oh I see it. Yeah you're on the right track.

Pseudothing!
input 2 numbers
while num1 < num2
if num1 less than num2, and is odd num1 adds 1
a string here appends the value of num1
else if num1 is even, add 2 to num1
a string here appends the value of num1

Take note that the appenders appear two times. Why? You need to get the 1st even value printed right? It would only do it once. So I suggest putting the check if the number is even outside the while loop since it would make the number even for once and after that it would stay even.

EDIT: responded to wrong post, sorry

This post has been edited by jon.kiparsky: 21 November 2011 - 09:03 PM

### #10 BaumbachR

Reputation: 0
• Posts: 13
• Joined: 20-November 11

Posted 21 November 2011 - 09:06 PM

jon.kiparsky, on 21 November 2011 - 08:54 PM, said:

BaumbachR, on 21 November 2011 - 10:39 PM, said:

ok i dont have my sample code on me at the moment but im wanting to write a while loop that adds all of the even numbers between two numbers the user inputs.(using JOptionPane) (1st num must be greater than 2nd num)
for an example, user inputs
numOne = 1;
numTwo = 20;

the program would then determine if first number was even if not it adds one, the sum of the even numbers between 1 and 20(i dont want it to add the equivalence of numTwo or over it i want it to stop on the even number before it)
would be 2+4+6+8+10+12+14+16+18=90. the sum would then be output to a string to be displayed in the JOptionPane.
it would determine if it was even by
if (numOne % 2 == 0)
correct?
i am just wanting some feedback code and ideas because i am having a few problems with my code(once again i dont have it with me) not adding up the right numbers.
Any help would be much appreciated

Yes, exactly the same as your previous problem of collecting odd numbers.

well yes but im having a problem with the code adding up the even numbers correctly but my code is on my laptop thats not currently with me but in a post written above this i did a quick coding thats close to what i have.

ChillyWilly, on 21 November 2011 - 08:59 PM, said:

In that case when you set your conditional statement(if), numOne<numTwo. Making it just less than instead of less than OR EQUAL it wont execute the statement when the last number(numTwo) is reached. In your example it would get to 14+16+18 then add 2 to numOne which makes it 20, then numOne would not be less than numTwo and wont execute again. Hope this helps.

the problem arises when lets say input was 1 and 21 the number would get to 20 and add 2 more because numOne is still less than numTwo

### #11 jon.kiparsky

• Pancakes!

Reputation: 8750
• Posts: 15,066
• Joined: 19-March 11

Posted 21 November 2011 - 09:09 PM

Quote

while numOne is less than numTwo //making sure first number is less than second if not and error is given
if numOne isnt even //making it odd
numOne++;
if numOne is even
numOne += 2; //keeps adding two until it gets to the maximum

The last if is not needed.Either it was odd, and you added one, so it's even now, or it was even, and you didn't change it, so it's still even. Sorted.

This post has been edited by jon.kiparsky: 21 November 2011 - 09:10 PM

### #12 pbl

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

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

Posted 21 November 2011 - 09:16 PM

Seems to me like a lot of discussions for a very basic loop in hypothetical code.
If the @OP had posted his actual code the problem would have been solved in a post or two.

### #13 ChillyWilly

Reputation: 16
• Posts: 41
• Joined: 21-October 11

Posted 21 November 2011 - 09:19 PM

'outputStr' needs a data type

'tempNum=counter' should be outside the while loop or else its going to equal the counter*2 each time it executes the loop.

'tempNum+=counter' should come before 'counter+=2' or else your going to add 2 to the counter and then add it to the tempNum before testing it against the while loop and the if statement

here are the revisions
numOne = 1; //assuming user inputs one
numTwo = 20; //assuming user inputs twenty
counter = numOne; //assigns a counter
tempNum = 0; //keeps sum as loop progresses
temp = 0; //to store starting number may not use
String outputStr; //to put the sum of all of the even numbers into to output
tempNum = counter;

while (counter < numTwo) { //checks to make sure first is less than second
if (counter %2 == 0) { //checks for even number
tempNum += counter;
counter += 2; //adds two to even number

} else {
counter++
}
outputStr = tempNum;
}

### #14 pbl

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

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

Posted 21 November 2011 - 09:22 PM

kind of rediculous to do that at each loop iteration

if (counter %2 == 0) { //checks for even number

do it only once before starting the while()
if(counter % 2 != 0) counter++;
while(counter < .....

### #15 ChillyWilly

Reputation: 16
• Posts: 41
• Joined: 21-October 11