Sum of Odd Digits of a Number in Java

Previously we have written a 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.

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

Procedure to calculate the sum of odd digits in a number in Java,

  1. Take a number
  2. Declare a variable oddDigitSum 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 odd or not
  5. If it is odd then add it to oddDigitSum variable, else go to next step
  6. Remove the last digit of the number
  7. Repeat the 3 to 6 steps until the number becomes 0.
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
      scan.close();
  }
}

The output for the different test-cases:-

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

Enter an integer number:: 0123456789
The sum of odd digits of the number 123456789 = 25

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 odd 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 findOddDigitSum(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 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 *