Java Program to Find Sum of Even Digits in a Given Number

Previously we have written a Java program to find the sum of digits of a given number. Now in this post, we will write a Java program to find the sum of even digits in a given number.

Procedure to find the sum of even digits in a given number,

  1. Take a number
  2. Declare a variable evenDigitSum to store the sum value and initialize it with 0
  3. Find the last digit of the number
  4. Check that the last digit is even or not
  5. If it even then adds it to evenDigitSum variable, else go to next step
  6. Remove the last digit of the number
  7. Repeat 3 to 6 steps until the number becomes 0
import java.util.Scanner;

public class SumOfEvenDigitsProgram {

  public static int findEvenDigitSum(int number) {

    // declare variables
    int lastDigit = 0;
    int evenDigitSum = 0;

    // loop to repeat the process
    while(number!=0) {

       // find last digit
       lastDigit = number%10;

       // check last digit even?
       if(lastDigit % 2 == 0) 
           // add it to sum
           evenDigitSum += lastDigit;

       // remove last digit of number
       number = number / 10;

    // return sum value
    return evenDigitSum;

  public static void main(String[] args) {

     // declare variables
     int number = 0;
     int sumOfEvenDigits = 0;

     // create Scanner class object 
     // for reading the values
     Scanner scan =  new Scanner(;

     // read inputs
     System.out.print("Enter an integer number:: ");
     number = scan.nextInt();

     // find sum of digits of number
     sumOfEvenDigits = findEvenDigitSum(number);

     // display result
     System.out.println("The sum of even digits of"+
       " the number "+number+" = "+ sumOfEvenDigits);

     // close Scanner class object

The output for the different test-cases are:-

Enter an integer number:: 1245
The sum of even digits of the number 1245 = 6

Enter an integer number:: 123456789
The sum of even digits of the number 123456789 = 20

In this program, we used a while loop. While loop is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

Also See:-

Instead of using a while loop, we can also use for loop to develop java program to find the sum of even digits in a given number. The for loop is also a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

public static int findEvenDigitSum(int number) {
     int sum, lastDigit;
     for(sum=0; number!=0; number/=10){
         lastDigit = number%10;
         if(lastDigit % 2 == 0)
             sum += lastDigit;
     return sum;

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *