#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?
#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…
Other two reasons are:
Not printing new line
Additional comma in the scanf’s format specifier
no new line required!!!
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
i++
is not inside the if block, it’s outside of it
oh yaa…sorry…my bad!!!