C++求输入正整数的位数,逆序输出

求输入正整数的位数,逆序输出

方法一:
知识点:if语句,while循环

//要求:给出一个不多于5位的正整数
//1)求出是几位数 2)分别打印出每一位数字 3)按逆序打印出各位数字,如原数123,输出321
#include<iostream>
using namespace std;
int main()
{
	long int n;
	cout << "请输入0—99999之间的数字:";
	cin >> n;
	while (n < 0 || n>99999)
	{
		cout << "输入错误,请再次输入0—99999之间的数字:";
		cin >> n;
	}
	int ten_thousand, thousand, hundred, ten, ones;
	ten_thousand = n / 10000;
	thousand = n / 1000;
	hundred = n / 100;
	ten = n / 10;
	//ones = n;
	int  w, q, b, s, g,x;
	w = n / 10000;
	q = n / 1000 % 10;
	b = n / 100 % 10;
	s = n / 10 % 10;
	g = n % 10;
	if (ten_thousand)
	{
		cout << n << "是" << 5 << "位数" << endl;
		/*w = n / 10000;
		q = n / 1000 % 10;
		b = n / 100 % 10;
		s = n / 10 % 10;
		g = n % 10;*/
		cout << "万位数为:" << ten_thousand << endl;
		cout << "千位数为:" << q << endl;
		cout << "百位数为:" << b << endl;
		cout << "十位数为:" << s << endl;
		cout << "个位数为:" << g << endl;
		x = g * 10000 + s * 1000 + b * 100 + q * 10 + ten_thousand;
		cout << "逆序数为:" << x << endl;
	}
		
	else if (thousand)
	{
		/*q = n / 1000 ;
		b = n / 100 % 10;
		s = n / 10 % 10;
		g = n % 10;*/
		cout << n << "是" << 4 << "位数" << endl;
		cout << "千位数为:" << thousand << endl;
		cout << "百位数为:" << b << endl;
		cout << "十位数为:" << s << endl;
		cout << "个位数为:" << g << endl;
		x = g * 1000 + s * 100 + b * 10 + thousand;
		cout << "逆序数为:" << x << endl;
	}
		
	else if (hundred)
	{
		
		cout << n << "是" << 3 << "位数" << endl;;
		cout << "百位数为:" << hundred << endl;
		cout << "十位数为:" << s << endl;
		cout << "个位数为:" << g << endl;
		x = g * 100 + s * 10 + hundred;
		cout << "逆序数为:" << x << endl;
	}
		
	else if (ten)
	{
		cout << n << "是" << 2 << "位数" << endl;
		cout << "十位数为:" << ten << endl;
		cout << "个位数为:" << g << endl;
		x = g * 10 + ten;
		cout << "逆序数为:" << x << endl;
	}
		
	else
	{
		cout << n << "是" << 1 << "位数" << endl;
		cout << "个位数为:" << g << endl;
		x = g ;
		cout << "逆序数为:" << x << endl;
	}
		
	return 0;

}

方法二:
知识点:if语句,switch语句

#include<iostream>
using namespace std;
int main()
{
	long int num;
	int indiv, ten, hundred, thousand, ten_thousand, place;
	cout << "enter number 0-99999:";
	cin >> num;
	if (num > 9999)
		place = 5;
	else if (num > 999)
		place = 4;
	else if (num > 99)
		place = 3;
	else if (num > 9)
		place = 2;
	else
		place = 1;
	cout << "place = " << place << endl;
	//计算各位数字
	ten_thousand = num / 10000;
	thousand = (num - ten_thousand * 10000) / 1000;
	hundred = (num - ten_thousand * 10000 - thousand * 1000) / 100;
	ten = (num - ten_thousand * 10000 - thousand * 1000 - hundred * 100) / 10;
	indiv = num - ten_thousand * 10000 - thousand * 1000 - hundred * 100 - ten * 10;
	cout << "original order:";
	switch (place)
	{
	case(5):cout << ten_thousand << "," << thousand << "," << hundred << "," << ten << "," << indiv << endl;
		cout << "reverse order:";
		cout << indiv << ten << hundred << thousand << ten_thousand << endl;
		break;
	case(4):cout << thousand << "," << hundred << "," << ten << "," << indiv << endl;
		cout << "reverse order:";
		cout << indiv << ten << hundred << thousand << endl;
		break;
	case(3):cout << hundred << "," << ten << "," << indiv << endl;
		cout << "reverse order:";
		cout << indiv << ten << hundred << endl;
		break;
	case(2):cout << ten << "," << indiv << endl;
		cout << "reverse order:";
		cout << indiv << ten << endl;
		break;
	case(1):cout << indiv << endl;
		cout << "reverse order:" << indiv << endl;
		break;
	}

	return 0;
  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值