# How to count even and odd digits in an integer?

Page 1 of 1

## 6 Replies - 11244 Views - Last Post: 08 November 2011 - 03:28 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=254868&amp;s=3fa77aad2d85e5a2bae562fcde41ddc0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 subone

Reputation: 0
• Posts: 4
• Joined: 29-October 11

# How to count even and odd digits in an integer?

Posted 08 November 2011 - 02:30 PM

Hello,

How can I count how many digits in an inputted integer are even and how many are odd?

for example if the user entered number: 1234567
The output should be: 3 digits are even and 4 digits are odd.

I think I have to use a loop, but I don't know what to put the condition.
Any help?

```                int num = 0;
int countEven = 0;
int countOdd = 0;
num = num%10;
if(num/2==0){
countEven++;
}
else{
countOdd++;
}
System.out.print(countEven+" "+countOdd);
```

Is This A Good Question/Topic? 0

## Replies To: How to count even and odd digits in an integer?

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11312
• Posts: 42,605
• Joined: 27-December 08

## Re: How to count even and odd digits in an integer?

Posted 08 November 2011 - 02:32 PM

You will want to use the modulus % operator instead of the division / operator. The modulus returns a remainder, while division does not. And a number is even if x%2 == 0.

### #3 Cancos

Reputation: 8
• Posts: 66
• Joined: 04-February 09

## Re: How to count even and odd digits in an integer?

Posted 08 November 2011 - 03:02 PM

Hope it helps, cheers!

```	int num = 123456789;
int countEven = 0;
int countOdd = 0;
for(int i = 1; i <= Integer.toString(num).length(); i++) {
if(i % 2 == 0)
countEven++;
else
countOdd++;
}
System.out.println("There are: " + countEven + " even and " + countOdd + " odd!");
}

```

Allow me to be more descriptive, my intentions were not to hand out code like candy.

Since we are not allowed to count the length for our "num" variable, we will have to convert it into a String. This will allows us to use the .length() method in order to get the proper length corresponding to your "num" value. In the loop, we are simply iterating through all the values "num" and checking as to whether our current position is odd or even, if so up the counter until there are no more elements to iterate through. Hope this helps.

This post has been edited by Cancos: 08 November 2011 - 03:12 PM

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: How to count even and odd digits in an integer?

Posted 08 November 2011 - 03:11 PM

Cancos, on 08 November 2011 - 06:02 PM, said:

Hope it helps, cheers!

```	int num = 123456789;
int countEven = 0;
int countOdd = 0;
for(int i = 1; i <= Integer.toString(num).length(); i++) {
if(i % 2 == 0)
countEven++;
else
countOdd++;
}
System.out.println("There are: " + countEven + " even and " + countOdd + " odd!");
}

```

This code is an horror and won't work
- The test for % 2 is done on the loop index not ion the actual number so the number of even and odd will always be equal or will differ by 1
- It is a real waste of time to call Integer.toString() in a loop passing always the same element

Despite we do not like to give code fast like that, you can't go away with the one proposed here so here it is

```    		while(num != 0) {
int rightDigit = num % 10;
if(rightDigit % 2 == 0)
countEven++;
else
countOdd++;
num /= 10;
}

```

*Edited actually even more simple
```    		while(num != 0) {
if(num % 2 == 0)
countEven++;
else
countOdd++;
num /= 10;
}

```

This post has been edited by pbl: 08 November 2011 - 03:21 PM

### #5 Cancos

Reputation: 8
• Posts: 66
• Joined: 04-February 09

## Re: How to count even and odd digits in an integer?

Posted 08 November 2011 - 03:17 PM

I was only trying to help out, I understand that I am no coding guru; however, currently I am trying to work on small coding tasks in order to further educate myself and get a better feel for these sort of things. If the code was wrong I apologize.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: How to count even and odd digits in an integer?

Posted 08 November 2011 - 03:24 PM

At least test it before posting it.

Can you imagine the time a newbie can waste trying to figure out what is wrong with cut & paste code assumed right from here ?

### #7 Cancos

Reputation: 8
• Posts: 66
• Joined: 04-February 09

## Re: How to count even and odd digits in an integer?

Posted 08 November 2011 - 03:28 PM

I think I misunderstood the problem.

My thoughts were that he was trying to determine how many digits are in the odd and even position. Whoops....

What he was actually trying to determine is if the value is odd and even...

This is actually my first post on DIC that I posted trying to be helpful, I just read the guidelines and see as to why it was important for me not to paste my work. In fact, my code is wrong in determining if the value is odd or even =X