Help with FCTRL - C++

https://www.codechef.com/problems/FCTRL
So the code runs fine on my computer, however when I submit it I get a wrong answer. Could anyone point out where I’m making a mistake.

#include <bits/stdc++.h>
using namespace std;

int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);

    int count, temp, num, t_cases = 0;
    int i = 0;
    cin >> t_cases;

    while (t_cases--)
    {
        cin >> num;
        while (i <= num)
        {
            temp = i;
            while (temp > 0 && temp % 5 == 0)
            {
                temp = temp / 5;
                count++;
            }

            i += 5;
        }

        cout << count << endl;
    }
    return 0;
}
6
3
60
100
1024
23456
8735373
0
14
24
253
5861
2183837

I’m getting an output wrong message.

Hi @vishaal_02, Your logic is perfectly fine. Just set count and i variables to 0 in the starting of test case while loop, so that previous test case values or garbage values don’t affect the answer.

Yet another case where the compiler tells you at least part of what’s going wrong:

[simon@simon-laptop][12:12:34]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling vishaal_02-FCTRL.cpp
Executing command:
  g++ -std=c++17 vishaal_02-FCTRL.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
vishaal_02-FCTRL.cpp: In function ‘int main()’:
vishaal_02-FCTRL.cpp:29:17: warning: ‘count’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   29 |         cout << count << endl;
      |                 ^~~~~
Successful
1 Like