Microsoft Interview Question
1,272 Interview Reviews |
Back to all Microsoft Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer at Microsoft:
how many zeros are there in 100!
See more for this Microsoft Software Development Engineer Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (5)
two = five = 0;
For(i=0;i<101;i++)
while(i%2==0)
two++;
i=i/2;
while(i%5==0)
five++;
i=i/5;
if(two<five)
return two;
else
return five;
Any questions feel free to ask.
Helpful Answer?
Yes |
No
Inappropriate?
#include <iostream>
using namespace std;
int main(){
int two=0, five=0, i;
int num;
for(i=2;i<101;i++)
{
num=i;
while(num%2==0)
{
two++;
num=num/2;
}
while(num%5==0)
{
five++;
num=num/5;
}
}
if(two<five)
cout << "100! has " << two << " trailing ceros.\n";
else
cout << "100! has " << five << " trailing ceros.\n";
return 0;
}
Helpful Answer?
Yes |
No
Inappropriate?
2 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
We should start again with a couple of observations:
- 0's are generated by 2*5 indeed
- we do not need to count the 2's because for every number the number of 2's is > number of 5's, so only how many 5's we have to 100 we need to count.
The solution would be as follows:
int numberOfTrailingZeros(int factorial) {
int result = 0;
while (factorial >= 5) {
result += (factorial\5);
factorial \= 5;
}
return result;
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate: