C 语言实例 – 判断数字为几位数

C语言教程评论

C 语言实例 - 判断数字为几位数

用户输入数字,判断该数字是几位数。

实例

#include <stdio.h>
int main()
{
    long long n;
    int count = 0;

    printf("输入一个整数: ");
    scanf("%lld", &n);

    while(n != 0)
    {
        // n = n/10
        n /= 10;
        ++count;
    }

    printf("数字是 %d 位数。", count);
}

运行结果:文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-panduanshuziweijiweishu.html

输入一个整数: 2345
数字是 4 位数。
文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-panduanshuziweijiweishu.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-panduanshuziweijiweishu.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 
公式库网
  • 本文由 公式库网 发表于 2021年12月7日19:59:58
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-yuyanshili-panduanshuziweijiweishu.html

发表评论