Definition
- Accumulator – a variable
productthat holds the running result, initialized to 1. - Multiplicative identity – 1 is the starting value because multiplying by 1 changes nothing.
- Counter – walks from 1 to N; each step does
product = product * i. - Empty product – when N is 0 the loop never runs and product stays at 1, giving 0! = 1.
- Contrast with sum – a sum loop seeds at 0 (additive identity); a product loop seeds at 1.
National Bank of India computes a compounding growth factor by multiplying a sequence of per-period multipliers together: the same loop shape as a factorial. For 4 periods the product is 1 * 2 * 3 * 4 = 24.
Understanding the Concepts
What is the product loop pattern?
Two variables: an accumulator called product and a counter called i. Product starts at 1, counter walks from 1 to N, and each step multiplies: product = product * i. When the loop ends, product holds N!.
package com.bank.service;
import java.math.BigInteger;
public class ProductLoop {
public static BigInteger factorial(int n) {
if (n < 0) throw new IllegalArgumentException("Negative input");
BigInteger product = BigInteger.ONE; // identity for multiplication
for (int i = 2; i <= n; i++) {
product = product.multiply(BigInteger.valueOf(i));
}
return product;
}
public static void main(String[] args) {
System.out.println("4! = " + factorial(4));
System.out.println("0! = " + factorial(0));
}
}
4! = 24
0! = 1
Why must the accumulator start at 1, not 0?
If you start product at 0, every multiplication gives 0. The first step would be 0 * 1 = 0, and the result can never recover. Starting at 1 means the first multiplication is 1 * 1 = 1, which correctly begins the accumulation.
// Wrong: starting at 0
int product = 0;
for (int i = 1; i <= 4; i++) product *= i;
System.out.println(product); // 0 (WRONG)
// Correct: starting at 1
int product = 1;
for (int i = 1; i <= 4; i++) product *= i;
System.out.println(product); // 24
How does the zero case work?
When N is 0, the loop condition i <= 0 is false immediately (i starts at 1 or 2), so the body never runs and product stays at its initial value of 1. That is exactly 0! = 1, requiring no special-case code.
How does this compare to a sum loop?
| Feature | Sum loop | Product loop |
|---|---|---|
| Accumulator name | sum | product |
| Identity value | 0 | 1 |
| Operation | + | * |
| Empty result | 0 | 1 |
| Banking use | total deposits | compounding factor |
How does this generalize to compounding?
Compounding interest is a product loop where instead of multiplying by the counter, you multiply the balance by (1 + rate) each period.
import java.math.BigDecimal;
import java.math.RoundingMode;
BigDecimal balance = new BigDecimal("100000.00"); // 1 lakh
BigDecimal rate = new BigDecimal("0.08"); // 8% annual
BigDecimal multiplier = BigDecimal.ONE.add(rate); // 1.08
for (int year = 1; year <= 5; year++) {
balance = balance.multiply(multiplier).setScale(2, RoundingMode.HALF_EVEN);
}
System.out.println("After 5 years: " + balance);
After 5 years: 146932.81
What are common mistakes?
// Wrong: seeding product at 0
BigInteger product = BigInteger.ZERO; // everything becomes 0!
// Wrong: seeding sum at 1
int sum = 1; // off by one for every total!
What should a senior engineer know?
The accumulator-with-identity idea is the same concept that functional programming calls a fold with an initial value. The identity element must match the operation: 0 for addition, 1 for multiplication, empty string for concatenation, true for AND, false for OR. Understanding this one principle unlocks every accumulation pattern.
What are common interview follow-up questions?
- What is the identity element for string concatenation? (empty string)
- How would you compute the product of an array of BigDecimal values?
- Can you express this loop as a Stream reduce?
Visual Overview
product = 1 (identity)
i = 1
i=1: product = 1 * 1 = 1
i=2: product = 1 * 2 = 2
i=3: product = 2 * 3 = 6
i=4: product = 6 * 4 = 24
Result: 24
Key Takeaway
The product loop is an accumulator-and-counter pattern where the accumulator starts at 1, the multiplicative identity, and is multiplied by each counter value. For N = 4, the product grows as 1, 2, 6, 24. The zero case needs no special code because the loop never runs and the product stays at 1, which is exactly 0!.
The single most important teaching point is the identity. For a product loop the seed is 1 because multiplying by 1 changes nothing. For a sum loop the seed is 0 because adding 0 changes nothing. Seeding a product at 0 collapses every result to 0, and seeding a sum at 1 makes every total off by one.
This pattern generalizes directly to compounding interest, where the balance is multiplied by (1 + rate) each period. Once you internalize the accumulator-and-identity idea, compounding and many other financial formulas become obvious.
In a banking project at National Bank of India, this exact loop shape computed compounding growth factors with BigDecimal, and the factorial variant demonstrated the accumulation to junior engineers. The identity-choice lesson prevented a bug where a new developer had seeded a compounding factor at 0.
Remember one sentence: a product loop starts at 1 because 1 is the multiplicative identity, and starting at 0 kills everything.
---