➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
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,
- Take a number
- Declare a variable
evenDigitSum
to store the sum value and initialize it with 0 - Find the last digit of the number
- Check that the last digit is even or not
- If it even then adds it to
evenDigitSum
variable, else go to next step - Remove the last digit of the number
- 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(System.in);
// 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
scan.close();
}
}
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:-
- Sum of digits of a number
- The sum of N Natural numbers
- Sum of odd digits in a number
- Sum of first & last digit of a number
- The Sum of Digits Until Single Digit
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!