HomePOSTSum of Odd Digits of a Number in Java Language

Sum of Odd Digits of a Number in Java Language

Sum of Odd Digits of a Number in Java Language (updated) – Java program to find the sum of digits of the given number. Now in this post, we will develop a program to calculate the sum of odd digits of a number in Java language program.

Example:- Number = 12345
Then the odd digits in a given number are 1, 3, 5 and therefore sum of odd digits = 1 + 3 + 5 = 9

<img width=

Procedure to calculate the sum of odd digits in a number

  • Take a number
  • Declare a variable oddDigitSum to store the sum value and initialize it with 0.
  • Find the last digit of the number
  • Check that the last digit is odd or not
  • If it is odd then add it to oddDigitSum variable, else go to next step
  • Remove the last digit of the number
  • Repeat the 3 to 6 steps until the number becomes 0.

Calculate the sum of odd digits in a number in JAVA

import java.util.Scanner;

public class SumOfOddDigitsProgram {

  public static int findOddDigitSum(int number) {

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

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

         // find last digit
         lastDigit = number%10;

         // check last digit odd?
         if(lastDigit % 2 != 0) {
            // add it to sum
            oddDigitSum += lastDigit;

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

      // return sum value
      return oddDigitSum;

  public static void main(String[] args) {

      // declare variables
      int number = 0;
      int sumOfOddDigits = 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
      sumOfOddDigits = findOddDigitSum(number);

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

      // close Scanner class object


Enter an integer number:: 12345
The sum of odd digits of the number 12345 = 9

Types of Sorting Algorithms:

Additional Reading


If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.

Thank you 😊 Keep Learning !



Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

- Advertisment -