Enormous input test

#include
using namespace std;
int main()
{unsigned long n,k,t;
scanf("%lu,%lu",&n,&k);
int i = 0;
int count = 0;
while(i < n)
{scanf("%lu",&t);
if (t%k==0)
count++;
i++;
}
printf("%lu",count);
}

Why is this giving wrong answer?

One of the reasons could be that you are incrementing the variable “i” inside the if block…I think it should be outside…hope this helps…:slight_smile:

Other two reasons are:

  1. Not printing new line
  2. Additional comma in the scanf’s format specifier

maybe the data type n is of unsigned long you are using and in the loop you are incrementing i and checking it with n which might be upto 10^7 . so try changing the data type of i and count from int to unsigned long and

The problem is in this line

scanf("%lu,%lu",&n,&k);

It should be

scanf("%lu %lu",&n,&k);

That is you should separate the %lu with a space and not a comma. It’s also best if you change i and count to unsigned long, just to be safe. Once you change that, your program gives the right output ( well it gave the right output for the conditions that I tried ).

Hope this solves your problem

2 Likes

no new line required!!!

i++ is not inside the if block, it’s outside of it

oh yaa…sorry…my bad!!!