Help to get desired form of output

+1 Akhilesh Chobey · November 25, 2015
I am writing a code to find number of zeroes in the factorial of a number.

It prints the answer on separate lines in each loop.
For e.g 1024! (factorial) has 255 zeros
So it prints 204,40,8,1.
How do I print the addition of these instead of multiple lines of answer.
Thanks
My code :

#include <stdio.h>
#include <stdlib.h>
#include<math.h>


int main()
{
int n;
printf("Enter the number:\n");
scanf("%d", &n);

int numberOfZeros;

int i;


for(i=1;i<200;i++){

if(pow(5,i)<n){

numberOfZeros=n/(pow(5,i));

printf("Number of zeros:%d\n", numberOfZeros);
}
else{
break;
}
}

system("pause");
return 0;

}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Dan P · November 25, 2015
Hi Akhilesh,

Change this line, numberOfZeros=n/(pow(5,i)); to
numberOfZeros = numberOfZeros + (n/(pow(5,i)));

or

numberOfZeros += n/(pow(5,i));

and then move the printf outside of the for loop.

:)
0 Akhilesh Chobey · November 25, 2015
Hey Dan, Thanks for your reply.
I made the changes you said:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>


int main()
{
    int n;
    printf("Enter the number:\n");
    scanf("%d", &n);

    int numberOfZeros;

    int i;


    for(i=1;i<200;i++){

    if(pow(5,i)<n){

    numberOfZeros+=n/(pow(5,i));
    }

    else{
        break;
    }
    }
    printf("Number of zeros:%d\n", numberOfZeros);


    system("pause");
    return 0;

}


However now it is giving quite unexpected answers. For example 3! has no zeroes. But it prints 2. Likewise 60! has 14 zeroes. But it prints 16.
+1 Dan P · November 25, 2015
Hi Akhilesh,

I don't understand what you are trying to do, so I can't help you with that.

If you give me a written example, then I will be able to help.

What I think you want is,

1. Enter a number, eg. 10.
2. 10 * 9 = 90
3. Does 90 have a zero? Yes, add to numberOfZeros, no move on.
4. 90*8 = 720
repeat 3.

Am I right?
0 Akhilesh Chobey · November 25, 2015
Oh my bad.
What I am trying is 5!=5*4*3*2*1=120. So it has one zero.
Likewise 10!=10*9*8*7*6*5*4*3*2*1=3628800. It has 2 zeroes.
Okay?
It also confuses me that when should printf be inside or outside a loop.
So if possible help me with that too.
Thanks a lot :)
+1 Dan P · November 25, 2015

#include
#include
#include
#include


int main()
{
        int n, nfac = 1;
        int numOfzeros = 0;
        printf("Enter the number:\n");
        scanf("%d", &n);


        for(;n > 0;n--){
                nfac *= n;
        }

        numOfzeros = countNumberOfZeros(nfac);

        printf("Result: %d\n Number of zeros: %d\n", nfac, numOfzeros);

        return 0;

}

int countNumberOfZeros(int result) {
        char buffer[32];
        int numberOfZeros = 0;
        int i;
        snprintf(buffer, 32, "%d", result);
        for(i = 0; buffer[i] != '\0'; i++) {
                if(!(strncmp(&buffer[i], "0", 1))) {
                        numberOfZeros++;
                }
        }

        return numberOfZeros;
}


This does what you want.

Although it is very limited, see if you can see why it's limited and come up with a solution on how to fix that?
0 Akhilesh Chobey · November 25, 2015
Okay. It will take me a while to interpret the code as I am still a newbie and also not acquainted with some of the syntax which 
you have used.
Anyways. Thanks for your help. 
:)
  • 1

C

107,149 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator