编程练习2

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

思路:分解出每一位上的数字。

代码:

 1 #include<iostream>
2 using namespace std;
3
4 int main()
5 {
6 int num;
7 cin>>num;
8
9 int wan = num / 10000;
10 int qian = num % 10000 / 1000;
11 int bai = num % 1000 / 100;
12 int shi = num % 100 / 10;
13 int ge = num % 10;
14
15 if (wan!=0)
16 cout<<"5位数: "<<ge<<shi<<bai<<qian<<wan<<endl;
17 else if (qian!=0)
18 cout<<"4位数: "<<ge<<shi<<bai<<qian<<endl;
19 else if (bai!=0)
20 cout<<"3位数: "<<ge<<shi<<bai<<endl;
21 else if (shi!=0)
22 cout<<"2位数: "<<ge<<shi<<endl;
23 else
24 cout<<"1位数:"<<ge<<endl;
25
26 return 0;
27 }

结果:



原文地址:https://www.cnblogs.com/qyddbear/p/2408459.html