Write a java program to check sum of odd digits.

Write a program to read a number, calculate the sum of odd digits (values) present in the given number. Here, we will use some approaches like while loop, for loop, do-while loop, and circular function(recursion).

Input and Output Format

  • Input consists of a positive integer n.
  • The output consists of a string. (For example, if the sum of digits is odd print ” Sum of odd digits is odd” else print “Sum of odd digits is even”)

Refer sample output for formatting specifications

Sample Input 1:

56895

Sample Output 1:

Sum of odd digits is odd.

Sample Input 2:

84228

Sample Output 2:

Sum of odd digits is even.

Java program to check sum of Odd digits using while loop

Following are the steps to check sum of the odd digits is even or odd:

  • Take a number as input.
  • Declare and initialize variable sum with 0.
  • Iterate over the number, and find the odd digits, add them and store it in the sum variable.
  • Next, just check if the sum is even or odd. If it is even just print “Sum of odd digits is even” else just print “Sum of odd digits is Odd”.
import java.util.Scanner;
public class Main {
  
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int n = input.nextInt();
    int sum=0;
    while(n>0){
      int rem = n%10;
      if(rem%2!=0){
        sum = sum+rem;
      }
      n = n/10;
    }
    
    if(sum%2==0){
      System.out.println("Sum of odd digits is even");
    }else{
      System.out.println("Sum of odd digits is odd");
    }
    
  }

}

Output

Check the Sum of Odd digits using for loop

Following are the steps to check the sum of the odd digits is even or odd:

  • Input a number.
  • Initialize the variable sum with 0.
  • Iterate over the number till it is greater than 0. Find the reminder and check if the remainder is odd then add it to the variable sum. Do this process till the number is completely divided.
  • Next, just check if the sum is even or odd. If it is even just print “Sum of odd digits is even” else just print “Sum of odd digits is Odd”.
package com.date;

import java.util.Scanner;

public class SumOfOddDigits {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int sum = 0;
        for (; n > 0; n = n / 10) {
            int rem = n % 10;
            if (rem % 2 != 0) {
                sum = sum + rem;
            }
        }
        if (sum % 2 == 0) {
            System.out.println("Sum of odd digits is even");
        } else {
            System.out.println("Sum of odd digits is odd");
        }
    }
}

Output

Check the Sum of Odd digits using the do-while loop

Following are the steps to check the sum of the odd digits is even or odd:

  • Input a number.
  • Initialize the variable sum with 0.
  • Iterate using a do-while loop over the number till it is greater than 0. Find the reminder and check if the remainder is odd then add it to the variable sum. Do this process till the number is completely divided.
  • Next, just check if the sum is even or odd. If it is even just print “Sum of odd digits is even” else just print “Sum of odd digits is Odd”.
package com.date;

import java.util.Scanner;

public class Main8 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int sum=0;
        do{
            int rem = n%10;
            if(rem%2!=0){
                sum = sum+rem;
            }
            n = n/10;
        }while(n>0);

        if(sum%2==0){
            System.out.println("Sum of odd digits is even");
        }else{
            System.out.println("Sum of odd digits is odd");
        }

    }
    }

Output

Java Program to check the sum of odd digits using recursion

We will be using recursion to solve this problem:

  • Input a number.
  • Call the checkSum() method and pass the number to it.
  • Inside the method, initialize the sum variable, now find the odd digits, and perform a recursive call to the method till the number is completely divided.
  • At last, return the value of the sum. If it is even just print “Sum of odd digits is even” else just print “Sum of odd digits is Odd”.
package com.date;

import java.util.Scanner;

public class SumOfOddDigits {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int sum = checkSum(n);
        if (sum % 2 == 0) {
            System.out.println("Sum of odd digits is even");
        } else {
            System.out.println("Sum of odd digits is odd");
        }


    }

    private static int checkSum(int n) {
        int sum = 0;

            int rem = n % 10;
            if (rem % 2 != 0) {
                sum = sum + rem;
                checkSum(n/10);
        }
            return sum;
    }
}

Output

Thus, in this way, we check if the sum of odd digits is even or odd in Java.