14 Replies - 3598 Views - Last Post: 21 November 2011 - 09:25 PM Rate Topic: -----

#1 BaumbachR  Icon User is offline

  • New D.I.C Head

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

Adding Even numbers

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

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Adding Even numbers

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.

Was This Post Helpful? 1
  • +
  • -

#3 ChillyWilly  Icon User is offline

  • New D.I.C Head

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

Re: Adding Even numbers

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

#4 BaumbachR  Icon User is offline

  • New D.I.C Head

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

Re: Adding Even numbers

Posted 21 November 2011 - 08:50 PM

View PostfromTheSprawl, 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

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Adding Even numbers

Posted 21 November 2011 - 08:54 PM

View PostBaumbachR, 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.
Was This Post Helpful? 1
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Adding Even numbers

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.
Was This Post Helpful? 1
  • +
  • -

#7 ChillyWilly  Icon User is offline

  • New D.I.C Head

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

Re: Adding Even numbers

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.
Was This Post Helpful? 1
  • +
  • -

#8 BaumbachR  Icon User is offline

  • New D.I.C Head

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

Re: Adding Even numbers

Posted 21 November 2011 - 09:00 PM

View PostChillyWilly, 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
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Adding Even numbers

Posted 21 November 2011 - 09:02 PM

View PostfromTheSprawl, 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

Was This Post Helpful? 0
  • +
  • -

#10 BaumbachR  Icon User is offline

  • New D.I.C Head

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

Re: Adding Even numbers

Posted 21 November 2011 - 09:06 PM

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

View PostBaumbachR, 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.

View PostChillyWilly, 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
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Adding Even numbers

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

Was This Post Helpful? 1
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Adding Even numbers

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

#13 ChillyWilly  Icon User is offline

  • New D.I.C Head

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

Re: Adding Even numbers

Posted 21 November 2011 - 09:19 PM

From your code corrections are:
'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;
}


Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Adding Even numbers

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

#15 ChillyWilly  Icon User is offline

  • New D.I.C Head

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

Re: Adding Even numbers

Posted 21 November 2011 - 09:25 PM

also a ';' is needed after 'counter++' in line 15 sorry i missed that :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1