Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

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

```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?
Please help me! Thank you in advance. I'm fairly new at this.  Reply With Quote

3. ## 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.  Reply With Quote

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

To be exact, the code to do this would look like this:

```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.  Reply With Quote