This code is supposed to read certain amount of numbers and then print how many of them are divisible by the divisor variable, however when i write it like this there seems to be some kind of problem.
#include <stdio.h>
int main()
{
long repeat;
int divisor;
long long t;
long result = 0;
scanf("%d", &repeat);
scanf("%d", &divisor);
for (long i = 0; i < repeat; i++)
{
scanf("%d", &t);
if (t % divisor == 0)
{
result++;
}
}
printf("%d",result);
return 0;
}
But when I set all of the variables at the start to 0 it works fine. Can somebody tell me what´s the problem with the first implementation?
#include <stdio.h>
int main()
{
long repeat = 0;
int divisor = 0;
long long t = 0;
long result = 0;
scanf("%d", &repeat);
scanf("%d", &divisor);
for (long i = 0; i < repeat; i++)
{
scanf("%d", &t);
if (t % divisor == 0)
{
result++;
}
}
printf("%d",result);
return 0;
}
long long t; .... scanf("%d", &t);should generate a warning. Insure all warnings are enabled and report the results. This will save you time.