Let's determine how many even digits and how many odd digits are in a number entered from the keyboard. If a number is divisible by 2 without a remainder (i.e. the remainder is zero), then it is even. And we'll increase the even digit counter (static variable even) by 1. Otherwise, the number is odd, so we'll increase the odd digit counter (static variable odd).

# Even and odd digits

- 10
Locked

Comments (25)

- Popular
- New
- Old

You must be signed in to leave a comment

Enrique Del Valle Backend Developer

9 January 2022, 19:19

extract each digit as a character, and then I get the numeric value. After that, I use the % operator to get the remaining value. If it is 0, I increase the even variable value. If not, I increase the odd variable. It works even with 0 digits in my local tests, but the server shows me the following:
-Be sure that the number of even digits is stored in the variable even.
-Be sure that the number odd digits is stored in the variable odd.
However, in my code, I have this:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String st = "";
st = reader.readLine();
int odd = 0;
int even = 0;
for (------){
if (----- % 2 == 0){
even++;
}else {
odd++;
}
}
System.out.println("Even: " + even + " Odd: " + odd);

0

Roman

10 January 2022, 07:36

Please post your question with the attached solution in the section Help.

0

Anonymous #10775689

28 July 2021, 16:31

Today I learnt that 0 is an even number 😄.
Is zero an even number?

0

ImDevin

10 April 2021, 04:02

pay attention to the example

0

Khongpak Phupkdee

13 March 2021, 14:50

Thank you for 10

0

Sinisa

25 February 2021, 10:26

At first I thought about converting int input to an array and than testing each array member, then I found out (not by myself 😎😆) that the last digit of every positive int is to be found by modulo 10. From that point on it is a matter of putting it in a loop, counting the odd/even digits and closing the loop so it does not run forever.

0

J H U X

24 January 2021, 19:05

Seriously, solving this task on my own in under 5 minutes is EXTREMELY SATISFYING. I'm happy this coding stuff is making sense now.

0

LennyMan

24 December 2020, 17:23

The solution with /10 its incredible smart and easy but i will never figured out by my own.

0

Vitalina

7 October 2020, 19:37

I solved it with ArrayList, modulus and loop for:) What's about you?

+2

Lex Icon

30 October 2020, 02:47

I solved it by (for int i ****) and 2 if-else conditions :)

+1

mastere

10 December 2020, 08:49

I solved it with a for loop, charAt method, and if statement with modulus. Using the for loop and CharAt to change the input text to characters and loop through them. Using the if statement and modulus to test for even or odd numbers.

+3

Pahunchik

5 May 2021, 12:57

first i read the number by Scanner, then find how many digits it has by loop with checking condition: if number / 10 = 0 or not, number / 100 = 0 or not etc. When get true - out from loop by break. Next loop i used for transformation number to digits by division it every loop to 10000, 1000, 100 ... minus extra part. If digit modulo to 2 > 0 then odd++ else even++

+2

Chloe Dinh

1 September 2020, 03:38

thanks you some comments about number 10. I was be able to make at the first try. ^_^!

+1

Ryan Vibbert

19 August 2020, 22:35

I got the correct solution here but I am curious how the loop ends.
The last line number = number/10 in the loop. How does it ever get the below 0 to end the while? I understand dividing by 10 removes a digit each time but with an example of 101. First run equals 101 = odd, second run 10 = even, third run 1 = even. But then to me the fourth run through the loop would have the number 1/10 which equals .1 which does not equal a remainder of 0. Shouldn't the loop continue really infinitely?

0

Roman

21 August 2020, 05:54

1/10 = 0
Java reads 1 / 10 as integer division which gives an answer of 0 (in integer division everything after the decimal is lopped off)

+2