How to find even digits out of a given number? eg:2475 | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

How to find even digits out of a given number? eg:2475

22nd Jun 2019, 7:42 AM
Marianna
6 Respuestas
+ 4
First take n as input Then while(n!=0) { d=n%10; if(d%2==0) System.out.println(d); n=n/10;} This will print the even nos of the no entered
22nd Jun 2019, 8:05 AM
Kishu
Kishu - avatar
+ 2
If x % 2 is 0, it is even.
22nd Jun 2019, 8:25 AM
Mahv
Mahv - avatar
+ 1
Thank you... Is there any other way?
22nd Jun 2019, 7:52 AM
Marianna
+ 1
Thanks ☺
22nd Jun 2019, 8:04 AM
Marianna
0
uhm, I’m pretty tired, but if you’re looking to find even digits in a whole number, all that comes to mind is variable manipulation haha: String num = "2475"; for(char c : num.toCharArray()){ int digit = Integer.parseInt(Character.toString(c)); if(digit % 2 == 0) System.out.println(digit); }
22nd Jun 2019, 8:03 AM
Jake
Jake - avatar
- 1
int[] nums = {2,4,7,5}; for(int num : nums) { if(num % 2 == 0) System.out.println(num); }
22nd Jun 2019, 7:51 AM
Jake
Jake - avatar