Back to Practice Dashboard
Python BasicsEasy
Last non-zero digit in factorial
Learn how to solve the 'Last non-zero digit in factorial' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function last_non_zero_digit(n) that returns the last non-zero digit of n! (n factorial). For example, 5! = 120, and the last non-zero digit is 2. The function takes a non-negative integer n and returns a single digit (1-9).
Constraints
- •0 <= n <= 1000
Examples
Example 1
Input
n = 5
Output
2
Explanation
5! = 120. Ignoring trailing zeros, the last non-zero digit is 2.
Example 2
Input
n = 10
Output
8
Explanation
10! = 3628800. The last non-zero digit is 8.
Example 3
Input
n = 1
Output
1
Explanation
1! = 1. The last non-zero digit is 1.
Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.