找到尾随零数的程序给出了错误的输出

时间:2017-08-23 00:59:22

标签: c

找到尾随零数的我的C程序输出错误。循环在1个测试用例后终止并返回垃圾输出。这是我的代码:

 #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;
 }     

2 个答案:

答案 0 :(得分:3)

您永远不会初始化number,然后您打印指向它的指针而不是数字本身。 当然你打算打印垃圾结果。

另外,我不明白你的算法应该如何工作。除以5并将其加到零的数量?如果我使用数字100,那将增加20,但100不会有20个尾随零。

答案 1 :(得分:2)

警告:格式'%d'需要类型为'int'的参数,但参数2的类型为'int *'

 printf(" %d \n", number);

您必须初始化它,int number =0;

或者你的意思是(?):

 scanf(" %d \n", &number);
相关问题