Here we will discuss SHL off campus coding question asked through METTI platform.
Question:
Trailing Zeros
Given ‘n'(1<=n<=10^6), find the number of trailing Zeros in the factorial of ‘n’. The factorial of ‘n’ is defined as the product of all integers from 1 to ‘n’.
Return the number of trailing zeros in the factorial of given number.
Input Specification:
Input1: An integer for which you have to calculate factorial.
Output Specification:
Return an integer representing the number of trailing zeros
Example 1:
Input1: 5
Output: 1
Explanation:
The factorial of 5 is 120. The number of trailing zeros is = 1
Example 2:
Input: 4
Output: 0
Explanation:
The factorial of 4 is 24. The number of trailing z


Algorithm To Solve
- Take input value from user that is N value
- First find out the factorial of the given number (try to store the result in long type of variable as result might be a big number).
- Using while loop keep dividing the number by 10 (N%10) which given last number as remainder, so when the remainder is equal to 0 (ZERO) increase a counter which indicates the number of trailing zeros.
- Continue the above step untill we reach res >= 10, so at this point break the for loop and get the result printed
JAVA CODE
package arrayPrograms; class Problem14 { public static void main(String[] args) { // Coded by VRASHIKESH PATIL int N=5; long res=1; int count=0; boolean flag=true; for(int i=1;i<=N;i++) { res=res*i; } while(flag) { if(res%10==0) { res=res/10; count++; } if(res<10) { flag=false; } } System.out.println(count); } }
Watch Complete Explanation for Above Code Below
Similar off Campus Questions
Accenture off campus coding question
Count Buildings Hacker Earth coding question with solution
Pingback: Saddle points in 2D array off campus problem solved - Is It Actually