2

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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
pop1912
  • 91
  • 1
  • 10
  • 2
    This is not the approach the task is assuming. The naive one will not work well here, you need to think about a non-trivial algorithm.As for your problem - you are likely simply overflowing the integer. – Eugene Sh. May 07 '19 at 14:49
  • 1
    It would be great if you could also tell us which inputs you have tried and what results you have received. The error could have been caused by numerous things. Edit: Of course there are easier ways to do this as @EugeneSh. stated. Think of it as a multiplication. What causes your result to have a 0 at the end? – Yiğit Aras Tunalı May 07 '19 at 14:49
  • You should think about another logic , since I think approach is not right. – Mr. Suklav Ghosh May 07 '19 at 14:55
  • 4
    To do this without overflowing you simply count every time you multiply by 5, e.g., in 25! you multiply by 5 twice for the 25, once each for 15, 10, and 5. So there will be 5 trailing zeros (note there are a surplus of multiples of 2, to turn the 5s into multiples of 10) – James Snook May 07 '19 at 14:55
  • @JamesSnook I'm not convinced that's sufficient, as it seems there can be other number combinations or sums of combinations that can generate additional trailing zeros, especially for larger numbers. – Andrew Henle May 07 '19 at 15:30
  • 1
    @AndrewHenle You can *only* get a multiples of ten by doing 5 * 2 and multiples of 10 are the only way to get trailing zeros. – James Snook May 07 '19 at 15:35

3 Answers3

6

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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

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

bruno
  • 32,421
  • 7
  • 25
  • 37
0
#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;
}
  • 3
    I suggest you add details on what the problem was before providing an answer. This way, people can learn and not just have solutions. – mw509 Mar 11 '20 at 13:34