Add Even and Odd Numbers

I have a programming assignment that needs to read a set of integers and then finds and prints the sum of the evens and sum of the odds.

I know how to find out which numbers are odd and even, I just can't figure out how to add up the evens and odds. Say a set of 10 integers is inputted, how can they be added up after it is determined if they are even or odd?

[374 byte] By [mattvgta] at [2007-11-27 9:58:07]
# 1
Well I take it you are storing these in an array? then why don't loop over the array and use the += operator?
_helloWorld_a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 2

> I have a programming assignment that needs to read a

> set of integers and then finds and prints the sum of

> the evens and sum of the odds.

>

> I know how to find out which numbers are odd and

> even, I just can't figure out how to add up the evens

> and odds. Say a set of 10 integers is inputted, how

> can they be added up after it is determined if they

> are even or odd?

Rather than us give you the answer, why not first give it a try. I suggest that you solve the problem first on paper. Then think about the steps that you had to take to create this solution. Translate those steps into java code, and you're there!

When done, post what you have (but please use code tags), and any specific questions you may have.

Good luck.

petes1234a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 3

take two vars for storing the sum of even and odd numbers.

.........

long odd,even;

if(oddnumber)

{

odd+=number;

}

else if(evennumber)

{

even+=number;

}

student@sunDNa at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 4
> take two vars for storing the sum of even and odd> numbers.> .........sudent, why not let her solve it?
petes1234a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 5

Here is my code after the tip from student... It compiles but once ran it doesn't do anything after the integers are put in.

import java.util.*;

public class EvenOddIntegers {

static Scanner console = new Scanner(System.in);

static final int limit = 10;

public static void main(String[] args) {

int number = 0, limit = 0, sumEven = 0, sumOdd = 0;

int odds = 0;

int evens = 0;

System.out.print("Enter ten positive integers: ");

while (limit <= 10)

{

number = console.nextInt();

}

if (number % 2 == 0)

{

sumEven += number;

}

else if (number % 2 != 0)

{

sumOdd += number;

}

System.out.println("Sum of even numbers is " + sumEven + ".");

System.out.println("Sum of odd numbers is " + sumOdd + ".");

}

}

mattvgta at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 6

Limit = 10 so why have this?

while (limit <= 10)

Also you if-statements need to be inside of your while-loop, you don't need to make the second check either, it will be either even or odd, just check one and use "else" instead of "else-if".

So far grade E-

:-)

EDIT: I just noticed that you have some variables which you are not using (odd, even) and two limits. You should use the final one but the name should be LIMIT and not limit.

Message was edited by:

_helloWorld_

_helloWorld_a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 7

>while (limit <= 10)

> {

>number = console.nextInt();

> }

what happens to number each time you step through this loop? It gets over-written. If you know how to use arrays, do that here (and in the rest of your program). If you don't know about arrays, read up on them. Also, it would be better for you to use a for loop here, not a while loop.

petes1234a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 8

while (limit <= 10)

{

number = console.nextInt();

}

if (number % 2 == 0)

{

sumEven += number;

}

else if (number % 2 != 0)

{

sumOdd += number;

}

this should be like this

while (limit <= 10)

{

number = console.nextInt();

if (number % 2 == 0)

{

sumEven += number;

}

else if (number % 2 != 0)

{

sumOdd += number;

}

}

student@sunDNa at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 9

> this should be like this

>while (limit <= 10)

> {

>number = console.nextInt();

>if (number % 2 == 0)

>{

>sumEven += number;

>}

>else if (number % 2 != 0)

>{

> sumOdd += number;

>}

>}

Sigh,.... must be trolling for Dukes. Again, let the OP figure this out for themself. Again, need to use a for loop, not while, again, I would recommend use of an array in case you need to display the numbers later.

petes1234a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 10
OP, you need to close your input stream also. Take a look at close() method for the scanner class.
_helloWorld_a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 11
I'm not trolling for anything, this is the first time I've posted on this forum. I've been using Java for 3 weeks, not quite understanding all of it.
mattvgta at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 12
> I'm not trolling for anything, this is the first time> I've posted on this forum. I've been using Java for 3> weeks, not quite understanding all of it.He was not referring to you. :-P
_helloWorld_a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 13

> I'm not trolling for anything, this is the first time

> I've posted on this forum. I've been using Java for 3

> weeks, not quite understanding all of it.

mattvgt, ease up please. Look at whose code I posted above my reply. It was that of student@sunDN, and my reply was addressed to him/her.

petes1234a at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 14
My apologies.
mattvgta at 2007-7-13 0:28:33 > top of Java-index,Java Essentials,Java Programming...
# 15
On further look, I guess you don't have to use an array unless your instructor wants you to display back the numbers entered.
petes1234a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 16
My problem is I can't get it to output the sum of the odds and evens. Our class has not done anything with arrays yet.
mattvgta at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 17
> My problem is I can't get it to output the sum of the> odds and evens. Our class has not done anything with> arrays yet.Did you read the previous posts?
_helloWorld_a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 18

who are thinking that I am replying bec I want dukes, so please clear this that I am not at all interested in this, Ok.

I just want that mattvgt's problem should be solved.

I think those who think that I am doing this for dukes they havent seen that I have did reply for those threads to which dukes stars are not available,Ok.

So anybody having this idea that all the ones here are doing reply bec they want to earn duke stars then please clear it.............

student@sunDNa at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 19

Yes, after I input the integers and hit Enter... I get nothing. I've tried a few of the suggestions. Here is my code now:

import java.util.*;

public class EvenOddIntegers {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {

int number = 0, limit = 0, sumEven = 0, sumOdd = 0;

System.out.print("Enter ten positive integers: ")

while (limit <= 10)

{

number = console.nextInt();

if (number % 2 == 0)

{

sumEven += number;

}

else if (number % 2 != 0)

{

sumOdd += number;

}

}

System.out.println("Sum of even numbers is " + sumEven + ".");

System.out.println("Sum of odd numbers is " + sumOdd + ".");

}

}

mattvgta at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 20

> who are thinking that I am replying bec I want dukes,

> so please clear this that I am not at all interested

> in this, Ok.

> I just want that mattvgt's problem should be solved.

> I think those who think that I am doing this for

> dukes they havent seen that I have did reply for

> those threads to which dukes stars are not

> available,Ok.

> So anybody having this idea that all the ones here

> are doing reply bec they want to earn duke stars then

> please clear it.............

But by providing your code, you are doing a disservice as you are not allowing the student to learn.

petes1234a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 21
> I just want that mattvgt's problem should be solved.You should leave that up to him, he is obviously doing some sort of home work and providing code for him will not teach him anything. Make him work for it, he will learn more.
_helloWorld_a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 22
> Yes, after I input the integers and hit Enter... I> get nothing. I've tried a few of the suggestions.> Here is my code now:How will your loop end? Where is your for loop? Again, get rid of the while and use for.Message was edited by:
petes1234a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 23

I used a for loop with the same results, what is it I'm overlooking?

import java.util.*;

public class EvenOddIntegers {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {

int number = 0, limit, sumEven = 0, sumOdd = 0;

System.out.print("Enter ten positive integers: ");

for (limit = 1; limit <= 10; limit++)

{

number = console.nextInt();

if (number % 2 == 0)

{

sumEven += number;

}

else if (number % 2 != 0)

{

sumOdd += number;

}

}

System.out.println("Sum of even numbers is " + sumEven + ".");

System.out.println("Sum of odd numbers is " + sumOdd + ".");

}

}

mattvgta at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 24
your program now works.
petes1234a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 25
Thanks for all your help!!!!!Enter ten positive integers: 1 2 3 4 5 6 7 8 9 10Sum of even numbers is 30.Sum of odd numbers is 25.Press any key to continue . . .Message was edited by: mattvgt
mattvgta at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 26
But dear friends what does class Scanner do here
Adi1000a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 27
> But dear friends what does class Scanner do hereWhat do you mean? It appears to be correctly getting his user input.
petes1234a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 28

Take a look below. This is basically what you have with some small improvements.

import java.util.Scanner; //Just import what you need

public class EvenOddIntegers {

public static void main(String[] args) {

Scanner console = new Scanner(System.in);

final int LIMIT = 10;

int number = 0;

int sumEven = 0;

int sumOdd = 0;

System.out.println("Please Enter a total of "+LIMIT+" numbers\n");

for (int i = 0; i < LIMIT; i++) {

System.out.print("Please Enter Number: "+i);

number = console.nextInt();

//If it is not even then it must be odd - second check not required.

if (number % 2 == 0)

{

sumEven += number;

}

else

{

sumOdd += number;

}

}

console.close(); //Important

System.out.println("Sum of even numbers is " + sumEven + ".");

System.out.println("Sum of odd numbers is " + sumOdd + ".");

}

}

_helloWorld_a at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 29
It is accepting the input. I've tested it with a variety of 10 different integers and it works. Thanks again!
mattvgta at 2007-7-21 23:12:47 > top of Java-index,Java Essentials,Java Programming...
# 30
_helloworld_ : Thanks for your input also.
mattvgta at 2007-7-21 23:12:52 > top of Java-index,Java Essentials,Java Programming...
# 31
One more question... Why should limit be LIMIT?
mattvgta at 2007-7-21 23:12:52 > top of Java-index,Java Essentials,Java Programming...
# 32

It is a naming convention for final variables. It is not mandatory but it is the norm.

int myVar = 0;

final int MY_VAR = 0;

// Now if I am reading some code and I come across MY_VAR as below I know it is final

if (10 > MY_VAR ) // I can tell this without checking where the class declares final int MY_VAR = 0;

It is designed to give indication to the casual reader that this variable is final without having to check how it was declared.

_helloWorld_a at 2007-7-21 23:12:52 > top of Java-index,Java Essentials,Java Programming...
# 33
Ok, didn't know that. Thanks :)
mattvgta at 2007-7-21 23:12:52 > top of Java-index,Java Essentials,Java Programming...
# 34
plaease help me with my assighment i nid 10 examples of a simple java programs.. can you send me some?
HeLp_The_PooRa at 2007-7-21 23:12:52 > top of Java-index,Java Essentials,Java Programming...
# 35

> plaease help me with my assighment i nid 10 examples

> of a simple java programs.. can you send me some?

to "Help the poor"

1) If you are starting a new topic, start a new thread. Don't piggy back to an existing thread.

2) There are hundreds of examples of simple programs here in this forum each week. Just start looking through the forums. Remember this is your work to do, not ours.

petes1234a at 2007-7-21 23:12:52 > top of Java-index,Java Essentials,Java Programming...