Java ProgramsControl FlowProduct of Digits

Product of Digits in Java

beginner·  Control Flow  ·  Loops

Problem

The product of a number's digits is found by peeling off one digit at a time and multiplying each one into a running total.

Given a number, find the product of its individual digits.

Input
1234
Output
Product of digits: 24

Java Program

Java
public class ProductOfDigits { public static void main(String[] args) { int n = 1234; int product = 1; // multiplying by 1 leaves the first digit unaffected while (n > 0) { product *= n % 10; n /= 10; } System.out.println("Product of digits: " + product); } }

Output

Product of digits: 24

Core Logic

Repeatedly pulling off the last digit with the modulo operator, and multiplying it into a running product, combines every digit without ever needing to know the number's length up front.

How It Works
  1. 1product starts at 1, since multiplying by 1 leaves the first digit unaffected.
  2. 2n % 10 extracts the current last digit of n.
  3. 3That digit is multiplied into product.
  4. 4n /= 10 removes the digit just processed, and the loop continues until n reaches 0.
For 1234, the digits 4, 3, 2, 1 are peeled off one at a time and multiplied to 24.
💡

Key Point: Starting product at 1 — not 0 — is what matters here, since multiplying anything by 0 would zero out the whole result.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: Each loop iteration strips exactly one digit off n, so the number of iterations equals n's digit count d, with only a running product kept.

Key Concepts

while loopmodulo operatordigit extraction

Approach 2: Java 8

Java
public class ProductOfDigitsStream { public static void main(String[] args) { int n = 1234; // Converts each digit character back to its numeric value, then folds them into a product int product = String.valueOf(n).chars().map(c -> c - '0').reduce(1, (a, b) -> a * b); System.out.println("Product of digits: " + product); } }

Output

Product of digits: 24

Core Logic

Treating the number as a string of digit characters lets a stream fold them into a product directly, without any manual arithmetic loop.

How It Works
  1. 1String.valueOf(n) converts the number into its digit string.
  2. 2.chars() streams each character's underlying code point.
  3. 3.map(c -> c - '0') converts each character code back into its numeric digit value.
  4. 4.reduce(1, (a, b) -> a * b) folds the stream of digits into a single product, starting from 1.
For 1234, the stream converts each character to its digit and multiplies them to 24, the same result the loop finds.
💡

Key Point: The identity value passed to reduce() plays the same role as the loop's initial product = 1 — both need to be the multiplicative identity for the fold to start correctly.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: The stream still visits each of the d digit characters once, converting and folding them without collecting anything.

Key Concepts

Streamchars()reduce()

Related Programs