Counting digits using while loop

前端 未结 7 597
清酒与你
清酒与你 2021-01-05 10:15

I was recently making a program which needed to check the number of digits in a number inputted by the user. As a result I made the following code:

int x;            


        
7条回答
  •  春和景丽
    2021-01-05 10:32

    If x is an integer, and by "built in function" you aren't excluding logarithms, then you could do

    double doub_x=double(x);
    double digits=log(abs(doub_x))/log(10.0);
    int digits= int(num_digits);
    

提交回复
热议问题