简体   繁体   中英

C program to find the trailing ZEROS at the end of a FACTORIAL of a given number

I have return the code to find a factorial and to display trailing zeros at the end of the factorial, but the output is wrong... could you please help me to find the mistake?

#include <stdio.h>

int main() {
    int m = 1, i, N, count = 0;

    scanf("%d", &N);

    for (i = 1; i <= N; i++) {
        m = m * i;
    }
    printf("%d", m);

    while (m > 0) {
        if ((m % 10) == 0) {
            count = count + 1;
            m = m / 10;
        }
        break;
    }
    printf("%d", count);

    return 0;
}

Your code only works for very small values of N : up to 9 . For slightly larger values, you would need to add an else keyword before the break statement and you would get a correct result for a few more cases.

For larger values, you must compute the power of 5 that divides the factorial. You can do this incrementally by summing the power of 5 that divide each individual number up to and including N .

#include <stdio.h>

int main() {
    int N, count;

    if (scanf("%d", &N) != 1)
        return 1;

    /* only consider factors that are multiples of 5 */
    count = 0;
    for (int i = 5; i <= N; i += 5) {
        for (int j = i; j % 5 == 0; j /= 5)
             count++;
    }
    printf("%d\n", count);
    return 0;
}

An even simpler and faster solution is this: compute the number of multiples of 5 less or equal to N , add the number of multiples of 5*5 , etc.

Here is the code:

#include <stdio.h>

int main() {
    int N, count;

    if (scanf("%d", &N) != 1)
        return 1;

    count = 0;
    for (int i = N; (i /= 5) > 0;) {
        count += i;
    }
    printf("%d\n", count);
    return 0;
}

you have two problems

  • your collapse the two outputs so you see only one of them / you cannot see who is who, just add a separator between them
  • an else is missing when you count so you count to only up to 1 and the result is wrong from factorial 10

So the minimal changes produce :

int main()
{
    int m=1,i,N,count=0;

    scanf("%d",&N);

    for(i=1;i<=N;i++)
    {
        m=m*i;
    }
    printf("%d\n",m); /* <<< added \n */

    while(m>0)
    {
      if((m%10)==0)
      {
        count=count+1;
        m=m/10;
      }
      else /* <<< added else */
        break;
    }
    printf("%d\n",count); /* <<< added \n */

    return 0;
}

after the changes :

pi@raspberrypi:/tmp $ ./a.out
5
120
1
pi@raspberrypi:/tmp $ ./a.out
10
3628800
2

Of course that supposes first you are able to compute the factorial without overflow

I also encourage you to check a value was read by scanf , checking it returns 1

#include <stdio.h>

int main()
{
    int n,i,f=1,t,c=0;
    printf("Enter  number ");
    scanf("%d",&n);
    t=n;
    for(i=1;t>=5;i++)
    {
        t=n/5;
        c=c+t;
        n=t;
    }
    printf("number of zeros are %d",c);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM