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://codercto.com/courses/l/17.html

查看所有标签

You Can Program in C++

You Can Program in C++

Francis Glassborow / John Wiley & Sons / 2006-7 / 406.80元

An interactive and fun way to learn C++, one of the most popular high-level programming languages for graphic applications This unique, hands-on approach to learning C++ makes t......一起来看看 《You Can Program in C++》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具