Help to get desired form of output

 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 zerosSo it prints 204,40,8,1.How do I print the addition of these instead of multiple lines of answer.ThanksMy code :#include #include #includeint 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)

Replies

 Dan P · November 25, 2015 Hi Akhilesh,Change this line, numberOfZeros=n/(pow(5,i)); tonumberOfZeros = numberOfZeros + (n/(pow(5,i)));ornumberOfZeros += n/(pow(5,i));and then move the printf outside of the for loop. 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 = 903. Does 90 have a zero? Yes, add to numberOfZeros, no move on.4. 90*8 = 720repeat 3.Am I right? 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? Akhilesh Chobey · November 25, 2015 Hey Dan, Thanks for your reply.I made the changes you said:#include #include #includeint 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)
• 1

C

129,680 followers