Introduction
In this post, I am going to write a program to find the sum of odd and even digits in a number in java. But before writing this program, I will write two individual programs i.e. the sum of odd digits and sum of even digits in number in java.
Also Read: Print 1 to 50 using do-while loop in Java
Let us understand what is this program? The answer is so simple. We need to read a number. Then using the modulo operator, we will separate each and every digit and check whether that number is even or odd. According to this, we will perform the addition. That is why I am writing three programs.
How do you find the sum of even digits in Java?
Here, we will separate the digit by dividing the number by 10 using the modulo operator and store the remainder in the variable rem.
We will check whether the value of this variable rem is divisible by 2 or not.
If it is divisible by 2, then perform addition. Now, see the program.
import java.util.*; class Even { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n, sum=0, rem; System.out.println("Enter your Number: "); int num=sc.nextInt(); n=num; while(num>0) { rem=num%10; if(rem%2==0) { sum=sum+rem; } num=num/10; } System.out.println("Sum of even digits in "+n+" is "+sum); } }
For the above program, see the output.

Here, the number is 12345 and the even digits are 2 and 4. So, the addition is 6.
Similarly, you can write a c program to find the sum of odd digits in a number.
How do you find the sum of odd numbers in Java?
Here, I am writing the same program with one change. That change is nothing but the remainder must not be even. How we are going to check this?
if(rem%2==1)
In the above statement, we are checking whether the remainder is 1 or not. If it is 1, that means that digit is odd.
I hope you got my point. Now, see the actual program.
import java.util.*; class Odd { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n, sum=0, rem; System.out.println("Enter your Number: "); int num=sc.nextInt(); n=num; while(num>0) { rem=num%10; if(rem%2==1) { sum=sum+rem; } num=num/10; } System.out.println("Sum of odd digits in "+n+" is "+sum); } }
See the output for this program.

If you have understood these two programs, you can easily write the third program i.e. sum of odd and even digits in a number in java.
Also Read: Java Program to Find Whether a Person is Eligible to Vote or Not
Sum of Odd and Even Digits in a Number in Java
We just have to combine the above two programs. Before writing the program, let us see the output first.

From the above output, we are reading the number 12345. But, this time we are printing the sum of both the digits in the same number. Now, see the program.
import java.util.*; class EvenOddDigits { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n, sumo=0,sume=0,rem; System.out.println("Enter your Number: "); int num=sc.nextInt(); n=num; while(num>0) { rem=num%10; if(rem%2==0) { sume=sume+rem; } else { sumo=sumo+rem; } num=num/10; } System.out.println("Sum of even digits in "+n+" is "+sume); System.out.println("Sum of odd digits in "+n+" is "+sumo); } }
I hope you have understood these programs. If you have any difficulties, you can contact me.
Thank you.