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.

Check Sum of Odd Digits in java

Input and Output Format: 
Input consists of a positive integer n.
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.

 

import java.util.Scanner;
public class Main {
  
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int n = input.nextInt();
    int summ=0;
    while(n>0){
      int remm = n%10;
      if(rem%2!=0){
        summ = summ+remm;
      }
      n = n/10;
    }
    
    if(summ%2==0){
      System.out.println("Sum of odd digits is even");
    }else{
      System.out.println("Sum of odd digits is odd");
    }
    
  }

}

Add a Comment

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

thirteen − ten =