Factorial question wrong answer despite of desired output

codeblocks
codechef
factorial
output
wrong-answer

#1

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

",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.


#2

scanf("%d",&n);
printf("%d ",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:


#3

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

#4

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


#5

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


#6

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


#7

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


#8

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


#9

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.


#10

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


#11

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


#12

The code worked after changing the specifiers.Thanks


#13

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.