Python BasicsEasy

Last non-zero digit in factorial

Detailed guide and Python implementation for the 'Last non-zero digit in factorial' problem.

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?
Consider using Recursion-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.