统计一个整数的位数。从键盘读入一个整数,统计该数的位数。

完整代码

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int number, bits = 0;
    printf("Input your number:");
    scanf("%d", &number);
    do {
        number = number / 10;
        bits++;
    } while (number != 0);
    printf("It contains %d digits.", bits);
    return 0;
}

运行结果1

Input your number:-99
It contains 2 digits.

运行结果2

Input your number:0
It contains 1 digits.