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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Odd Even Zero Counter?

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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:

    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);
      }
    }


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default 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.

    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).
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

Similar Threads

  1. String counter
    By chkang0130 in forum Algorithms & Recursion
    Replies: 9
    Last Post: December 8th, 2009, 11:56 AM