# Program help with counting even and odd integers in input value.

• July 3rd, 2010, 07:10 PM
fueledbyrick
Program help with counting even and odd integers in input value.
Code Java:

```import java.util.Scanner; public class Integer { //-------------------------------------------------------------------- // Read a value from the user and counts the number of odd and even integers in the value // ------------------------------------------------------------------- public static void main (String[ ] args) { int integer, oddinteger, eveninteger; Scanner scan = new Scanner (System.in); System.out.print (&#8220;Enter an integer: &#8221;)```

What I need the program to do is to read the value input by the user and identify the number of even and odd integers in the value. For example, if the user inputs 403 I need the program to output : Odd Integers: 2 Even Integers: 1.

Does that make sense?
• July 3rd, 2010, 07:46 PM
Norm
Re: Program help with counting even and odd integers in input value.
Do you have the algorithm yet for extracting a digit from a number? You need to figure out how to get 3 digits out of 403.
This will involve modulus and division.
Then look at the digits one by one to determine if they are odd or even.
• July 6th, 2010, 07:02 PM
aussiemcgr
Re: Program help with counting even and odd integers in input value.
To be exact, the code to do this would look like this:

Code :

```int num1 = 6;   if(num1%2==0) System.out.print("Even Number");   if(num1%2==1) System.out.print("Odd Number");```

Doing "int % 2" will give you either the number 0 or 1. If the result is 0, it means it is even. If the result is 1, it means it is odd.

Basically, mod ( % ) will grab the two number, divide them, and return the remainder. If you divide 4 by 2, you get 2 with a remainder of 0, so it returns 0. If you divide 3 by 2, you get 1 with a remainder of 1, so it returns 1.

You can do the same thing if you needed to find out if a number was divisible by something. For instance, if you needed to know if 40 was divisible by 10, you would simply code "40%10" and it would return 0, which would indicate that it is divisible by 0. If you did "45%10" it would return 5, because 5 is the remainder of 45/10.

I dont think I can explain it any more detailed then that. Once you get used to using mod, you will find out how extremely useful it is.