Number of even/odd numbers in an integer

Page 1 of 1

3 Replies - 2050 Views - Last Post: 11 May 2011 - 07:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=231649&amp;s=656e5252e2fb1f0efd52ff55f110b981&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Alpina94

Reputation: 0
• Posts: 5
• Joined: 02-February 11

Number of even/odd numbers in an integer

Posted 11 May 2011 - 06:58 PM

I would like to make a program that reads the number of even numbers, odd numbers and zeros within an integer (ie. 12230: 2 odds, 2 evens, 1 zero). I understand that you need to user the % operator, but I am getting confused on how to use it. If someone could help correct the code below, I would be very grateful! Thanks.

``` int evenCount = 0, oddCount = 0, zeroCount = 0, digit, userNum;
Scanner scan = new Scanner(System.in);

System.out.println("Enter an integer.");
userNum = scan.nextInt();

do
{
if(userNum % 2 == 0  && userNum != 0)
{
evenCount++;
}
else if(userNum == 0)
{
zeroCount++;
}
else
{
oddCount++;
}
digit = userNum%10;
userNum = userNum/10;
}
while(digit != 0);

System.out.println("The integer has " + evenCount + " even number(s).");
System.out.println("The integer has " + oddCount + " odd number(s).");
System.out.println("The integer has " + zeroCount + " zero(s).");
```

This code works incorrectly.

Is This A Good Question/Topic? 0

Replies To: Number of even/odd numbers in an integer

#2 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

Re: Number of even/odd numbers in an integer

Posted 11 May 2011 - 07:27 PM

The % operator gives you the remainder that results from doing integer division. For example, if you divide the integer 18 by the integer 10 the result is 1 remainder 8 -- in other words 10 "goes into" 18 once, and the remainder is 8.

So to separate out the digits of a multi-digit number num,
num % 10 will give you the last digit
and
num / 10 will give you an integer consisting of all the original digits excluding the last digit.

#3 Alpina94

Reputation: 0
• Posts: 5
• Joined: 02-February 11

Re: Number of even/odd numbers in an integer

Posted 11 May 2011 - 07:33 PM

r.stiltskin, on 11 May 2011 - 07:27 PM, said:

The % operator gives you the remainder that results from doing integer division. For example, if you divide the integer 18 by the integer 10 the result is 1 remainder 8 -- in other words 10 "goes into" 18 once, and the remainder is 8.

So to separate out the digits of a multi-digit number num,
num % 10 will give you the last digit
and
num / 10 will give you an integer consisting of all the original digits excluding the last digit.

Thank you, I understand that logic now. However, I did that in my project, yet it still returns incorrect values.

#4 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

Re: Number of even/odd numbers in an integer

Posted 11 May 2011 - 07:45 PM

Because in your do-while loop you keep testing the "evenness", etc., of usernum instead of digit.