In the previous article, we have seen Java Program to Find Sum of Even and Odd Digits of a Number
In this article we will see how we can find product of all even and odd digits of a number using Java programming language.
Java Program to Find Product of Even and Odd Digits of a Number
We have to find product 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 multiply it with odd digits and if it is even then multiply it with even digits and finally show the product of odd and product of even digits separately.
For example:
If a number is 67843
Then in this number even digits are 6, 8 and 4. So, product of even digits are 6*8*4=192
Odd digits are 7 and 3. So, product of odd digits are 7*3=21
Let’s see different ways to find product of all even and odd digits of a number.
Method-1: Java Program to Find Product 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
andoddSum
and initialize both to 1. - Using a while loop check each digit the number.
- If digit is even then multiply it to
evenProduct
- else multiply it to
oddProduct
- 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 evenProduct and oddProduct //and initializing both to 1 int evenProduct=1; int oddProduct=1; //continue loop till number becomes 0 while(number!=0) { //getting digit of number int rem=number%10; //if 'rem' is even digit then multiply it to evenProduct if(rem%2==0) { evenProduct=evenProduct*rem; } //else multiply it to oddProduct else { oddProduct=oddProduct*rem; } number=number/10; } System.out.println("Product of even digits: "+ evenProduct); System.out.println("Product of odd digits: "+ oddProduct); } }
Output: Product of even digits: 10 Product of odd digits: 12
Method-2: Java Program to Find Product 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
evenProduct
andoddProduct
and initialize both to 1. - Using a while loop check each digit the number.
- If digit is even then multiply it to
evenProduct
- else multiply it to
oddProduct
- 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 evenProduct and oddProduct //and initializing both to 1 int evenProduct=1; int oddProduct=1; //continue loop till number becomes 0 while(number!=0) { //getting digit of number int rem=number%10; //if 'rem' is even digit then multiply it to evenProduct if(rem%2==0) { evenProduct=evenProduct*rem; } //else multiply it to oddProduct else { oddProduct=oddProduct*rem; } number=number/10; } System.out.println("Product of even digits: "+ evenProduct); System.out.println("Product of odd digits: "+ oddProduct); } }
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
evenProduct
andoddProduct
and initialize both to 1. - Using a while loop check each digit the number.
- If digit is even then multiply it to
evenProduct
- else multiply it to
oddProduct
- 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 evenProduct and oddProduct //and initializing both to 1 int evenProduct=1; int oddProduct=1; //continue loop till number becomes 0 while(number!=0) { //getting digit of number int rem=number%10; //if 'rem' is even digit then multiply it to evenProduct if(rem%2==0) { evenProduct=evenProduct*rem; } //else multiply it to oddProduct else { oddProduct=oddProduct*rem; } number=number/10; } System.out.println("Sum of even digits: "+ evenProduct); System.out.println("Sum of odd digits: "+ oddProduct); } }
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.
Related Java Programs: