C 练习实例29

C 语言教程 · 2019-02-21 19:13:32

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

程序分析:学会分解出每一位数,如下解释。

实例

// Created by www.codercto.com on 15/11/9. // Copyright © 2015年 码农教程. All rights reserved. // #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); } }

以上实例输出结果为:

请输入 5 位数字:12345
为 5 位数,逆序为: 5 4 3 2 1

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

How to Think Like a Computer Scientist: Learning with Python

How to Think Like a Computer Scientist: Learning with Python

Allen B. Downey、Jeffrey Elkner、Chris Meyers / Green Tea Press / 2002-1-4 / USD 24.95

""How to Think Like a Computer Scientist"" is an introduction to programming using Python, one of the best languages for beginners. This is a Free Book -- you can download it from thinkpython.com. But......一起来看看 《How to Think Like a Computer Scientist: Learning with Python》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试