# Java Program to Find Product of Digits of a Number

In this program, we will learn to code the Java Program to Find Product of Digits of a Number. Let’s understand How to Find the Product of Digits of a Number in Java Programming Language. In previous programs, we have also discussed and learned to code the Java Program to add digits of a number. Using the same logic with some modification we can also code the Java Program to Find Product of Digits of a Number.

Contents

## Product of Digits of a Number

Suppose, you are given the number 12345, so the program should calculate the result of 1 * 2 * 3 * 4 * 5 = 120 and 120 is the desired output.

Now, Let’s see the program to code the Java Program to Find Product of Digits of a Number.

## Java Program to Find Product of Digits of a Number

```import java.util.*;
import java.lang.*;

class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = sc.nextInt();

int n = num; // copy of the original number

//Logic
int product = 1;

while(n>0){
int digit = n%10;

product *= digit;

n/=10;
}

System.out.println("Product of digits of the number "+num+" is "+product+" .");

}
}
```

Output

``````Enter the number: 12345
Product of digits of the number 12345 is 120 .``````

## How Does This Program Work ?

```		    Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = sc.nextInt();```

In this program, we take the number as input from the user using `Scanner` class in Java and store it in variable num of int datatype.

`int n = num; // copy of the original number`

Then, we preserve the original number and perform the calculation using the copy of the original number.

```                    //Logic
int product = 1;

while(n>0){
int digit = n%10;

product *= digit;

n/=10;
}```

Then, we calculate the product of digits of the number using the while loop.

Suppose, the user enters a number 5634, then:

• 1st While Loop Iteration: while (5634 != 0), the condition is True.
• digit = n % 10 = 5634 % 10 = 4
• product = product * digit = 1 * 4 = 4
• n = n / 10 = 5634 / 10 = 563
• 2nd While Loop Iteration: while (563 != 0), the condition is True.
• digit = n % 10 = 563 % 10 = 3
• product = product * digit = 4 * 3 = 12
• n = n / 10 = 563 / 10 = 56
• 3rd While Loop Iteration: while (56 != 0), this condition is also True.
• digit = n % 10 = 56 % 10 = 6
• product = product * digit = 12 * 6 = 72
• n = n / 10 = 56 / 10 = 5
• 4th While Loop Iteration: while (5 != 0), this condition is True.
• digit = n % 10 = 5 % 10 = 5
• product = product * digit = 72 * 5 = 360
• n = n / 10 = 5 / 10 = 0
• 5th While Loop Iteration: while (0 != 0), here the condition is False, so the while loop terminates and we get product = 360.
`System.out.println("Product of digits of the number "+num+" is "+product+" .");`

Then, we print the calculated area as the result using the `System.out.println()` function.

This is the Java Program to Find Product of Digits of a Number.

## Conclusion

I hope after going through this post, you understand the Java Program to Find Product of Digits of a Number. If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.