# Java Program to Find Sum of Even and Odd Digits of a Number

In this article we will see how we can find sum of all even and odd digits of a number using Java programming language.

## Java Program to Find Sum of Even and Odd Digits of a Number

We have to find sum of even and odd digits of a number. Means we have to check each number it is even or odd. If it is odd then add it with odd digits and if it is even then add it with even digits and finally show the sum of odd and sum of even digits separately.

For example:

If a number is 67843

Then in this number even digits are 6, 8 and 4. So, sum of even digits are 6+8+4=18

Odd digits are 7 and 3. So, sum of odd digits are 7+3=10

Let’s see different ways to find sum of all even and odd digits of a number.

### Method-1: Java Program to Find Sum of Even and Odd Digits of a Number By Using Static Input Value

Approach:

• Declare an integer variable ‘number‘ and initialize the value.
• Declare two integer variables evenSum and oddSum and initialize both to 0.
• Using a while loop check each digit the number.
• If digit is even then add it to evenSum
• else add it to oddSum
• Print the result.

Program:

public class Main
{
public static void main(String args[])
{
//an integr variable 'number' declared and initialized
int number = 6547;

//declaring two variables evenSum and oddSum
//and initializing both to 0
int evenSum=0;
int oddSum=0;

//continue loop till number becomes 0
while(number!=0)
{
//getting digit of number
int rem=number%10;

//if 'rem' is even digit then add it to evenSum
if(rem%2==0)
{
evenSum=evenSum+rem;
}
else
{
oddSum=oddSum+rem;
}

number=number/10;
}
System.out.println("Sum of even digits: "+ evenSum);
System.out.println("Sum of odd digits: "+ oddSum);
}
}
Output:

Sum of even digits: 10
Sum of odd digits: 12

### Method-2: Java Program to Find Sum of Even and Odd Digits of a Number By Using User Input Value

Approach:

• Declare an integer variable ‘number‘ and take the value as user input using Scanner class.
• Declare two integer variables evenSum and oddSum and initialize both to 0.
• Using a while loop check each digit the number.
• If digit is even then add it to evenSum
• else add it to oddSum
• Print the result.

Program:

import java.util.*;

public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

//taking a number input from user
System.out.println("Enter a number: ");
int number = sc.nextInt();

//declaring two variables evenSum and oddSum
//and initializing both to 0
int evenSum=0;
int oddSum=0;

//continue loop till number becomes 0
while(number!=0)
{
//getting digit of number
int rem=number%10;

//if 'rem' is even digit then add it to evenSum
if(rem%2==0)
{
evenSum=evenSum+rem;
}
else
{
oddSum=oddSum+rem;
}

number=number/10;
}
System.out.println("Sum of even digits: "+ evenSum);
System.out.println("Sum of odd digits: "+ oddSum);
}
}
Output:

Enter a number:
88325
Sum of even digits: 18
Sum of odd digits: 8

### Method-3: Java Program to Find Sum of Even and Odd Digits of a Number By Using User Defined Method

Approach:

• Declare an integer variable ‘number‘ and initialize the value.
• Call a user defined method evenOdd() and pass that number as parameter.
• Inside method declare two integer variables evenSum and oddSum and initialize both to 0.
• Using a while loop check each digit the number.
• If digit is even then add it to evenSum
• else add it to oddSum
• Print the result.

Program:

import java.util.*;

public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

//taking a number input from user
System.out.println("Enter a number: ");
int number = sc.nextInt();
//calling user defined method evenOdd()
evenOdd(number);
}

//evenOdd() method to find sum of even and odd digits
public static void evenOdd(int number)
{
//declaring two variables evenSum and oddSum
//and initializing both to 0
int evenSum=0;
int oddSum=0;

//continue loop till number becomes 0
while(number!=0)
{
//getting digit of number
int rem=number%10;

//if 'rem' is even digit then add it to evenSum
if(rem%2==0)
{
evenSum=evenSum+rem;
}
else
{
oddSum=oddSum+rem;
}

number=number/10;
}
System.out.println("Sum of even digits: "+ evenSum);
System.out.println("Sum of odd digits: "+ oddSum);
}
}
Output:

Enter a number:
4365
Sum of even digits: 10
Sum of odd digits: 8

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.