Product of Digits with an Explicit Zero Case
Calculate digit products with an explicit zero branch. Students contrast empty-loop behavior with a valid zero result while preserving negative-input handling.
1# Take number2num = int(input("Enter a number: "))34# Calculate product5product = 16temp = abs(num)7if temp == 0:8 product = 09else:10 while temp > 0:11 digit = temp % 1012 product *= digit13 temp //= 101415print(f"Product of digits: {product}")
Output
Enter a number: 234 Product of digits: 24
In this one
- Separate zero from an empty product
- Multiply digit values
- Scan negative magnitudes
How it works
- 1
If temp is zero, set product zero.
- 2
Otherwise multiply every extracted digit into product.
- 3
Remove one digit per iteration and print.
Edge cases
Why does input zero need a branch?
Why does input zero need a branch?
Its while loop would not run, leaving the initial product one.
What does 203 produce?
What does 203 produce?
Zero, because one of its digits is zero.
What's going on
This version decides the zero case before entering its digit loop. That prevents the multiplication identity one from being mistaken for the product of zero's single digit.
For nonzero magnitudes, every remainder ten is multiplied into product before integer division removes the digit. A zero anywhere inside the number also forces the final product to zero. abs makes -234 and 234 share the same product.
Why does input zero need a branch? Its while loop would not run, leaving the initial product one.
What does 203 produce? Zero, because one of its digits is zero.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does -234 produce?
Pick an answer to check it.
Question 2
Why does input zero need a branch?
Pick an answer to check it.
Questions people ask
Why does input zero need a branch?
Its while loop would not run, leaving the initial product one.
What does 203 produce?
Zero, because one of its digits is zero.
What does -234 produce?
24.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum