# Odd Even Zero Counter?

• February 19th, 2010, 04:19 AM
velop
Odd Even Zero Counter?
I'm trying to create three counters for the number of zeros, odds and even digits entered, but I'm having a little trouble. My code is below:

Code :

```import java.util.Scanner; public class OddEven { //-------------------------------------------------------------------------- // Displays the odd, even, and zero digits in an entered value. //-------------------------------------------------------------------------- public static void main(String[] args) { Scanner Scan = new Scanner(System.in); System.out.println ("\n\nThis program will find and display the amount of"); System.out.println ("odd, even and zero digits in the entered value."); System.out.print ("Enter a value: "); int num = Scan.nextInt(); if(num == 0) zeroCounter++; else if (num % 2 == 0) evenCounter++; else oddCounter++; System.out.println("Zero digits: " + zeroCounter); System.out.println("Even digits: " + evenCounter); System.out.println("Odd digits: " + oddCounter); } }```
• February 19th, 2010, 01:13 PM
CT&SC
Re: Odd Even Zero Counter?
Well, what the code is doing at present is reading in a number, and telling you if the number is zero, odd, or even. It does nothing about the individual digits of the number.

You need a way to read in the number, num, and then (in a loop, likely) peel off the digits (in base 10, I'm guessing) and use your code on each digit. Here's some pseudocode for what I think you need to do.

Code :

```CountDigits(number : integer) 1. zero := 0 2. odd := 0 3. even := 0 4. number := |number| 5. while number > 0 6. do digit := number % 10 7. if digit = 0 then zero := zero + 1 8. else if digit % 2 = 1 then odd := odd + 1 9. else even := even + 1 a. number := floor(number / 10) ! integer division, so round down... floor b. return (zero, odd, even)```

This code takes as input an integer and returns an ordered triple (zero, odd, even) with the counts of the zero, odd, and even digits. Notice that this code works for positive and negative numbers and for the number zero does not count any digits at all (this can be changed by including it as a special case, if you want zero=1 when number=0).