Number of 0 after factorial

Source: Internet
Author: User

Title Description:

How many 0 are behind the factorial of n?

the factorial of 6 = 1*2*3*4*5*6 = 720,720 is followed by a 0. Input
A number n (1 <= n <= 10^9)
Output
Number of outputs 0

The 0 at the end of each number factorial is caused by multiplying the factor 2 and the factor 5 in the preceding factorial number, and because there are more than 5:2, just consider the number of 5.
Every 5 numbers, will produce a 0, such as 5, 10, 15 ...;
Every 25 number, will produce more than a 0, that is, two 0, such as 25/50/75 ...;
Every 125 number, will produce more than a 0, that is, three 0, such as 125, 625 ... and so on.
That is, each time the number is divided by 5, the number after the factorial is obtained.

The code is as follows:
1#include <stdio.h>2 intMain (void)3 {4UnsignedintA, B =0;5scanf"%d", &a);6      while(A! =0){    7A = A/5;8B + =A;9     }Tenprintf"%d", b); One     return 0; A}

Number of 0 after factorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.