w3resource

Java: Check whether every digit of a given integer is even


Check If All Digits in Integer Are Even

Write a Java method to check whether every digit of a given integer is even. Return true if every digit is odd otherwise false.

Note: 1, 3, 5, 7, 9 are odd digits and 0, 2, 4, 6, and 8 are even digits

Sample data:
(8642)->true
(123)->false
(200)->true

Pictorial Presentation:

Java Method Exercises: Check whether every digit of a given integer is even
Java Method Exercises: Check whether every digit of a given integer is even

Sample Solution:

Java Code:

import java.util.Scanner;
public class Main { 
 public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Input an integer:");
        int n = in.nextInt();
        System.out.print("Check whether every digit of the said integer is even or not!\n");
        System.out.print(test(n));
        }

  public static boolean test(int n){
    final int f = 10;
    if (n == 0){
        return false;
    }
    while(n != 0){
        if((n % f) % 2 != 0){
            return false;
        }
        n /= 10;
    }
     return true;
   }
}

Sample Output:

Input an integer: 8642
Check whether every digit of the said integer is even or not!
true

Flowchart :

Flowchart: Display the factors of 3 in a given integer


For more Practice: Solve these Related Problems:

  • Write a Java program to check if all digits in an integer are odd.
  • Write a Java program to determine if an integer has alternating even and odd digits.
  • Write a Java program to check if an integer consists solely of even digits and is a multiple of 2.
  • Write a Java program to verify if the sum of all even digits in an integer equals a given value.

Go to:


PREV : Display Factors of 3 in Integer.
NEXT : Check If All Characters Are Vowels.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.