• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sum of even numbers in WhileLoop

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For my intro programming class, this assignment requests the use of a while loop. The objective is to input 2 integers (firstNum, secondNum), verify the first value is smaller than the second, and then output the odd integers between the two input values
(inlcuding the input if it is odd). After outputting those, it wants us to output the sum of even integers between (and including if input was even) the inputs.

The formatting guide that came with our assignment uses test variables 5 and 21, so I have been testing my program with the same values. The odd numbers populate perfectly. The issue I'm running into is that when it's time to add the sum of the integers the value is not correct. If I enter even values 6 & 20 I get a sum of 91, and if I enter 5 and 21 I get a value of 118, neither of which are the correct value (104, which they should both be.) This is what I have for my program right now. I'm really not sure where to go/what to do to rectify this issue.



Edit: Editing to mention the Sqr variables above are for a second part of the program I have not written yet, but are irrelevant to this loop. (it is in a second whileloop).
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all Welcome to Ranch Emily
You can use something like this while loop for counting even integers between two numbers if the first number is smaller than second one..

 
Emily Clark
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, good to be here.

Would you recommend having this second segment as a separate instance from the first? So, the first whileLoop offers the odd counting, then this is an entirely separate instance?
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sankalp, I think moderators will agree with me that it is not a good thing to provide a working code as a solution. OP should get the solution by brainstorming himself/herself.

Hi Emily, You want to add the even values, but you are making "counter" a odd value in line 31 and 37 and using that to add the values, so you are getting wrong output.
 
Emily Clark
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By setting the seven integers in the separate while loop it created the correct output - I see why the results were originally odd by using the same counter. Odd and Even should have been two separate counters and parameters from the beginning.

Thanks you guys!
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tapas Chand wrote:Hi Sankalp, I think moderators will agree with me that it is not a good thing to provide a working code as a solution. OP should get the solution by brainstorming himself/herself.

Hi Emily, You want to add the even values, but you are making "counter" a odd value in line 31 and 37 and using that to add the values, so you are getting wrong output.


Hello Tapas, it's not a solution to her problem. It's the simple use of while loop. that small code nowhere related to her logic. If she understands the use of while loop from what I've provided, she can herself modify her own code
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sankalp, from what I can see, you did in fact give Emily code that is a partial solution to her problem. We discourage this kind of "helping" around here, first because we are not a code mill and second because we want students to do their own homework. There are, in fact, schools that consider submitting answers you got off the internet or even just asking for answers on the internet as academic misconduct that can get all the way up to plagiarism. The university that my son goes to has such policies which is why I have to be very careful about how I help him when he's doing his homework assignments. Please keep this in mind for future reference. Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic