Task 6: Create a function in Java that counts the number
of even digits in a positive integer.
public static int countEvenDigits(int a); // Ex,
countEvenDigits(1234) => 2
Task 7: Create a generic function that returns digits in
a given range. The ones place corresponds to power 0, the tens
place corresponds to power 1, the hundreds place corresponds to
power 2, and so on. Think 10^3 => 1000 => thousands place.
(Do not use exponents in your code.)
// Ex, getDigits(12345,0,0) => 5
public class CountEvenDigits { public static int countEvenDigits(int a) { int count = 0; if (a == 0) { count = 1; } else { while (a > 0) { if ((a % 10) % 2 == 0) { count++; } a /= 10; } } return count; } public static void main(String[] args) { System.out.println(countEvenDigits(1234)); } }
Get Answers For Free
Most questions answered within 1 hours.