Product of Digits Recursively
Calculate product of digits recursively.
IntermediatePhase 3 - Level 2: Advanced RecursionExample 4 of 10
product-of-digits-recursively.py
1def product_digits(n):2 # Base case3 if n == 0:4 return 156 # Recursive case7 last_digit = n % 108 if last_digit == 0:9 return 0 # Product becomes 010 return last_digit * product_digits(n // 10)1112# Test13num = int(input("Enter a number: "))14result = product_digits(abs(num))15print(f"Product of digits: {result}")
Output
Enter a number: 234 Product of digits: 24
What's going on
Multiply last digit with product of remaining.
Key Concepts:
Base case: n == 0, return 1
Extract last digit
Multiply with recursive result
Handle zero digit (product becomes 0)
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum