C 练习实例29

C语言教程评论

C 练习实例29

题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

程序分析:学会分解出每一位数,如下解释。文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili29.html

实例

#include <stdio.h>

int main( )
{
    long a,b,c,d,e,x;
    printf("请输入 5 位数字:");
    scanf("%ld",&x);
    a=x/10000;        /*分解出万位*/
    b=x%10000/1000;   /*分解出千位*/
    c=x%1000/100;     /*分解出百位*/
    d=x%100/10;       /*分解出十位*/
    e=x%10;           /*分解出个位*/
    if (a!=0){
        printf("为 5 位数,逆序为: %ld %ld %ld %ld %ld\n",e,d,c,b,a);
    } else if(b!=0) {
         printf("为 4 位数,逆序为: %ld %ld %ld %ld\n",e,d,c,b);
    } else if(c!=0) {
         printf("为 3 位数,逆序为:%ld %ld %ld\n",e,d,c);
    } else if(d!=0) {
         printf("为 2 位数,逆序为: %ld %ld\n",e,d);
    } else if(e!=0) {
         printf("为 1 位数,逆序为:%ld\n",e);
    }
}

以上实例输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili29.html

请输入 5 位数字:12345
为 5 位数,逆序为: 5 4 3 2 1
文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili29.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili29.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 最后更新:2021-12-7
公式库网
  • 本文由 公式库网 发表于 2021年12月6日20:13:46
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-lianxishili29.html

发表评论