Factorial question wrong answer despite of desired output

Here is the code for the question

#include<stdio.h>
int main(){
     long int n;
     long int i=5;
     long int z=0,j=0;
    int testCase;
     scanf("%d",&testCase);

    for(j=0;j<testCase;j++){
        scanf("%d",&n);
        for(;n/i!=0;i*=5){
           z+=n/i;
        }
             printf("%d\n",z);
             z=0;
             i=5;
    }

  return 0;
}

I have checked the output corresponding to different inputs in codeblocks and it showed correct output but codechef shows wrong answer. PLease help me to find my mistake.

scanf("%d",&n);
printf("%d\n",z);

You are using wrong specifiers. %d is for int variables ONLY. This mismatch of specifiers in scanf and printf is causing your program to print garbage value. Fix this and you will get an AC. :slight_smile:

Hey @namanjn,

I think the mistake is in the test case where the number is greater than the range of int since you are using %d instead of %lld in your code. For a better explanation, you can check my


[1]. 

Hope this helps!

  [1]: https://www.codechef.com/viewsolution/8970982

It kind of fails even for numbers in range of int because of wrong specifier. It printed garbage values for all 6 sample inputs.

but i have run this code on codechef and it ran fine for several inputs.

In codeblocks its running fine.Can i see the input and output as obtained by codechef compiler

Did you check it for cases greater than 10^7?

yeah, it will work fine until you enter the values in the range of int, after that it will give WA.

What decides your verdict is how your code runs in codechef environment. Your PC might make adjustments, give a more friendly environment to your code. But here it might be different.

you can also check my code if you have any doubt.

Use “Code, Compile and Run” at practice tab, its the compiler where you can check your code in codechef IDE/environment

The code worked after changing the specifiers.Thanks

Hey @namanjn, if you believe that your question is answered please mark it as answered otherwise it’ll appear on CodeChef discuss unanswered question. Thanks.

//