6 Replies - 6823 Views - Last Post: 08 November 2011 - 03:28 PM Rate Topic: -----

#1 subone  Icon User is offline

  • New D.I.C Head

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

  • Self-Trained Economist
  • member icon




Reputation: 10646
  • View blog
  • Posts: 39,539
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Cancos  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • 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

Was This Post Helpful? -1
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

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

Posted 08 November 2011 - 03:11 PM

View PostCancos, 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

Was This Post Helpful? 0
  • +
  • -

#5 Cancos  Icon User is offline

  • D.I.C Head

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

#6 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • 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.
Must admit that with 123456789 the answer would have been right :)

Can you imagine the time a newbie can waste trying to figure out what is wrong with cut & paste code assumed right from here ?
Was This Post Helpful? 0
  • +
  • -

#7 Cancos  Icon User is offline

  • D.I.C Head

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

Page 1 of 1