☕ Java Program to Count Even Digits in a Number | Interview Program | Find Even Digit in Java | Find Odd Digit in Java
In this video, you will learn how to count even digits in a number using Java step by step.
We’ll understand:
✔ Scanner input
✔ Modulus concept
✔ Digit separation logic
✔ Loop processing
✔ Java program
✔ Final output
This Java interview program tutorial is specially designed for beginners and freshers.
🧠 Problem Statement
User enters a number:
Example:
12345
Program must count how many digits are even.
🧠 Logic
Read full number
Run loop for total digits
Extract last digit using modulus 10
Check digit divisible by 2
If yes → increase count
Remove last digit by dividing number by 10
Repeat until all digits processed
✅ Java Program (Class Name CountEvenDigit)
import java.util.Scanner;
class CountEvenDigit
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
int count = 0;
int rem = 0;
System.out.println("Enter Number");
int num = obj.nextInt();
for(int i = 1; i is less than or equal to 5; i++)
{
rem = num % 10; // gets last digit
if(rem % 2 equals zero)
{
count++;
}
num = num / 10; // removes last digit
}
System.out.println("Number of Even: " + count);
}
}
(Use less-than-equal and equals operators in real Java.)
🧠 Step by Step Explanation
Step 1 – Input
User enters:
12345
Step 2 – Extract Last Digit
rem = num % 10
Gives last digit:
12345 → 5
Step 3 – Check Even
rem % 2 equals zero
If digit is even → increase count.
Step 4 – Remove Last Digit
num = num / 10
12345 becomes 1234.
Step 5 – Loop Continues
Repeats for:
4
3
2
1
Step 6 – Final Output
Prints total even digits.
⭐ Sample Input
12345
⭐ Output
Number of Even: 2
(Even digits are 2 and 4)
🧠 Simple Understanding
%10 → get digit
/10 → remove digit
%2 → check even
⭐ Why Interviewers Ask This?
✔ Digit extraction logic
✔ Modulus understanding
✔ Loop processing
✔ Mathematical thinking
⭐ Key Takeaways
✔ Uses %10 and /10
✔ Processes digit by digit
✔ count stores even digits
✔ Core interview logic
⚠ Common Beginner Mistakes
❌ Forgetting num division
❌ Wrong loop count
❌ Confusing rem and num
❌ Missing Scanner import
📚 What You’ll Learn Next
Sum of Digits
Reverse Number
Palindrome
👍 Like
💬 Comment doubts
🔔 Subscribe for Java interview tutorials
java count even digits
count even digits java
java interview programs
digit programs in java
java logical programs
#Java
#JavaInterviewPrograms
#CountEvenDigit
#JavaBasics
#LearnJava
#Freshers
#Programming
#InterviewPreparation