Count Trailing Zeros in Factorial of a Number | Coding Interview | Maths | Python 🐍 & C++

Ganesh Prasad
4 min readJul 22, 2022
Trailing Zeros in n!.

This is the second program under the mathematics category that can be asked during the initial phase of your interview. It is a simple problem to code if you know basic maths; for example, in this case, you need to know how we get zeros in a factorial of a number (in what conditions).

If you are preparing for an interview, consider solving these problems.

Let’s begin.

Definition

Given a positive integer n, we have to find the number of trailing zeros of its factorial, n!.

The factorial of a number n is the product of all the numbers from n to 1.

n! = n x (n-1) x (n-2) x … 3 x 2 x 1

For example, the factorial of 6 ( 6 x 5 x 4 x 3 x 2 x 1) is 720, and the number of trailing zeros is 1.

Example 1:
Input
: n = 5
Output: 1
Explanation: The factorial of 5 (5!) is 120, hence the number of trailing zeros is 1.

--

--

Ganesh Prasad
Ganesh Prasad

Written by Ganesh Prasad

Backend Developer at Appscrip | C++ veteran, 💜 Dart

No responses yet