Number of Trailing Zeros

By   Tewodros   Date Posted: Feb. 27, 2024  Hits: 265   Category:  Algorithm   Total Comment: 0             A+ A-


side

Given any positive integer, we want to know how many trailing 0s does it have.

Example

1000 Ans. 3

4500 Ans 2

Solution

In order to find this we just need to see how many times the number can be divided by 5

public class Solution {

   public int TrailingZeroes(int n) {

       int count = 0;

       while (n > 0) {

           n /= 5;

           count += n;

       }

       return count;

   }

}

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* aaydpa

Back to Top