3 Replies - 2050 Views - Last Post: 11 May 2011 - 07:45 PM Rate Topic: -----

#1 Alpina94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Alpina94  Icon User is offline

  • New D.I.C Head

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

Re: Number of even/odd numbers in an integer

Posted 11 May 2011 - 07:33 PM

View Postr.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.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1