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

In the previous article, we have discussed about Java Program to Find Sum of Geometric Progression

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

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

We have to find product of 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 product between sum of odd and sum of even digits.

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

So the product between sum of even digits and sum of odd digits = 18 * 10 = 180

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

Method-1: Java Program to Find Product of Sum of Odd Digits and Even Digits of a Given 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
  • Now find the product between evenSum and oddSum and 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 add it to oddSum
            else
            {
                oddSum=oddSum+rem;
            }
            
            number=number/10;
        }
      System.out.println("Sum of even digits: "+ evenSum);
      System.out.println("Sum of odd digits: "+ oddSum);
      
      //find product between oddSum and evenSum and print result
      System.out.println("Product between Sum of odd digits and Sum of even digits are: "+ (oddSum*evenSum));
      
   }
}
Output:

Sum of even digits: 10
Sum of odd digits: 12
Product between Sum of odd digits and Sum of even digits are: 120

Method-2: Java Program to Find Product of Sum of Odd Digits and Even Digits of a Given 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
  • Now find the product between evenSum and oddSum and 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 add it to oddSum
            else
            {
                oddSum=oddSum+rem;
            }
            
            number=number/10;
        }
      System.out.println("Sum of even digits: "+ evenSum);
      System.out.println("Sum of odd digits: "+ oddSum);
      
      //find product between oddSum and evenSum and print result
      System.out.println("Product between Sum of odd digits and Sum of even digits are: "+ (oddSum*evenSum));
      
   }
}
Output:

Enter a number: 
5344
Sum of even digits: 8
Sum of odd digits: 8
Product between Sum of odd digits and Sum of even digits are: 64

Method-3: Java Program to Find Product of Sum of Odd Digits and Even Digits of a Given 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
  • Now find the product between evenSum and oddSum and 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 add it to oddSum
            else
            {
                oddSum=oddSum+rem;
            }
            
            number=number/10;
        }
      System.out.println("Sum of even digits: "+ evenSum);
      System.out.println("Sum of odd digits: "+ oddSum);
      
      //find product between oddSum and evenSum and print result
      System.out.println("Product between Sum of odd digits and Sum of even digits are: "+ (oddSum*evenSum));
      
   }
}
Output:

Enter a number: 
73
Sum of even digits: 0
Sum of odd digits: 10
Product between Sum of odd digits and Sum of even digits are: 0

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

Related Java Programs: