Reputation: 125
My C program to find trailing number of zeroes is giving wrong output. The loop gets terminated after 1 test case and returns garbage outputs. Here is my code:
#include <stdio.h>
#include <math.h>
int main()
{
int testcase, no_of_zeroes, i, c, number;
scanf(" %d \n", &testcase);
for(i = 0; i<testcase; i++)
{
no_of_zeroes = 0;
printf(" %d \n", &number);
c = 5;
while((number/c) > 0)
{
no_of_zeroes += (number/5);
c *= 5;
}
printf(" %d \n", no_of_zeroes);
}
return 0;
}
Upvotes: 0
Views: 84
Reputation: 4714
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’
printf(" %d \n", number);
you must initialize it, int number =0;
or maybe you mean(?):
scanf(" %d \n", &number);
Upvotes: 2
Reputation: 17040
You're never initializing number
, and then you're printing a pointer to it instead of the number itself. Of course you're going to print garbage results.
Also, I don't understand how your algorithm is supposed to work. Dividing by five and adding that to the number of zeroes? If I did that with the number 100, that would add 20, but 100 doesn't have 20 trailing zeroes.
Upvotes: 3