How to add only even numbers with a while loop

Status
Not open for further replies.

jgonzo

Honorable
Oct 5, 2012
133
0
10,690
I have a homework assignment that I have spent entirely to much time on. Read through the book, been trying to use google for help on tutorials, and have turned no luck. I know how to set the even numbers from the odd number with the mod, but have no idea on a appropriate way to just add the even numbers.

The program must be between the numbers 1 and 100 (already stated that properly I believe), then computes the sum of all even numbers within that parameter. The output of the sum must be after the loop.

Here is what I got so far:

int number = 1;
int sum = 0;

while (number >= 1 && number <= 100)

if (number % 2 == 0)


 
Well now you know how to find the even numbers you just add them up. Not the easiest way to solve the sum, but it's an artificial problem. If you are really having problems with this then you need to discuss it with your teacher as I think you are failing to grasp the fundamentals of programming.
 

jgonzo

Honorable
Oct 5, 2012
133
0
10,690
Ironically she won't help. I figured it out, but the class is frustrating. I enjoy doing stuff like this, but the teacher fails to actually teach it. She says it's an exploratory class and you are supposed to figure it out on your own. I disagree with this completely since the only kids doing good are the ones that have taken classes in programming before. Everytime I ask for some help she tells me to copy the smart kids program. PS: This is a intro. course to programming.

 

jgonzo

Honorable
Oct 5, 2012
133
0
10,690
Honestly I feel like the university will take no efforts to persue my complaints. Just a waste of time. Ya, it is very frustrating that I can locate no help with that course. I have even tried the help lab, but the instructors in the room will just "half ass" helping you while doing their own stuff on their computers. It's honestly a joke. I have nowhere to turn for with help :'(
 

randomizer

Champion
Moderator
Well you won't know that until you try. You have the right to complain about the lack of involvement as you're paying good money for them to teach you, not surf the net. If they don't want to do anything then perhaps you can ask for a refund :)
 
I appreciate that you have a problem with the quality of teaching at your University. I was actually amazed to hear that you are a University student and that this was not a question from an elementary High School course. But that is a matter that you must address else you are going to cintinue having difficulties.

As to the question you ask, if you can't see how to do this, and with all the Internet and book resources available to you cannot work out how to do it, then you are on the wrong course. Computer programming is not for you and will only lead to further confusion. Perhaps this is what the teacher is trying to tell you. Consider another course.

If you want to continue, and can't address the teaching problem, buy one of those "Teach Yourself in 24 Hours" or "For Dummies" books on computer programming
 

GoldenI

Distinguished
Nov 11, 2010
445
0
18,810
When you are provided with a seemingly endless source of information (the Internet), then it really negates the need to ask the teacher for help... That being said, she is paid to teach... and if she does not answer your questions that you feel deserve to be answered, talk to the head of the department, or simply the head of the University. Universities take complaints like this seriously, you know.
 

jgonzo

Honorable
Oct 5, 2012
133
0
10,690
Randomizer- You right. I have been thinking about doing something, but will have to look into the appropriate individual to speak with.
Ijack-It is an intro. course that was the lowest level provided. Most universities do in fact offer elementry level courses for those that have never done anything of the sort before; i.e. webpage design, engineer drawing, etc... I have looked into the program youself books, but the teacher will freak out when you use program language that is not exactly what she wants. She is very strange in a lot of ways with her teaching the more I think about it.
GoldenI- I have tried searching the internet for help, but nothing seems to help me with the exact problem I am having. Might be similar, but different so it really neglects to help my situation. Yes, it may be a good idea for me to speak with the dept. head about this situation.
 

jgonzo

Honorable
Oct 5, 2012
133
0
10,690
One more questions, please try to help with the coding here. The assignment is due tomorrow and I have figured out the majority of the program, but am completely aloof with this next part.

Step 1) Write a prgram with a while loop that computes the sum of all even number between 1 and 100 (inclusive). Output the sum after the loop. Print your code.

public static void main(String[] args) {
int number = 1;
int sum = 0;

while (number >= 1 && number <= 100){

if (number % 2 == 0)

sum = number + sum;
number ++;
}
System.out.println("The sum of all positive numbers is: " + sum);
}
}

*I know this part is correct, because I ran the loop with correct results*


Step 2) Modify the program so that the user is asked to enter the ending number greater than 10 (so that the caluclated sum is of all even numbers between 1 and the users entered value). Be sure your code ensures the user's input is greater than 10 before calculating the sum. Print your code.

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); //To get user input
int number = 1; //set to 1 because that's lowest value accepted
int userNumber = 0; //set to 0 so that the user input is based only on # entered
int sum = 0; //set to 0 since you start with no sum

System.out.println("Please enter a number greater than 10"); //display
number = keyboard.nextInt(); //sets value based on user input

while (number >= 1 && number <= userNumber){ //test parameter
if (number % 2 == 0 && userNumber % 2 == 0) //testing for even numbers
sum = sum + userNumber; //sum of even numbers
number++; //to ensure loop does last forever
}
System.out.println("The sum of all even numbers is: " + sum); //display sum
}
}


*I know that at the top of the coding you have to have the java scanner package, I left this out since all I need help with is the body of the coding*

This step above is where I have my issue. Can't figure out what to change the code to for the correct answer on the problem being asked. Any help would be great!
 

hdeezie80

Honorable
Jul 18, 2012
205
0
10,710
Well just for future reference when ever you are trying to tell if a number is odd or even just do % 2 modulus does remainder division so if you get 1 its odd 0 its even you can just put this in a function that returns a boolean

like this

bool isEven(int num)
{
if (num % 2 == 0)
return true;
else
return false;
}
 
Status
Not open for further replies.