给一个不多于5位的正整数,要求:

//(1)统计一个整数n是几位数
//例如:12345->5(12345是5位数);123456789->9(123456789是9位数)
0->1; -123456789->9**

//代码如下:
#include <stdio.h>
#include <math.h>
#include <ctype.h>

int GetFigures(int n)
{
if(n==0)
return 1;
int count = 0;
while(n!=0)
{
n/=10;
count++;
}
return count;
}

int main()
{
printf("%d\n",GetFigures(12345));
printf("%d\n",GetFigures(123456789));
printf("%d\n",GetFigures(0));
printf("%d\n",GetFigures(-123456789));

return 0;

}

运行分析:
在这里插入图片描述

//(2)逆序输出整数N的每一位数字
12345789->9 8 7 6 5 4 3 2 1
得到个位数n%10

//代码如下:
#include <stdio.h>
#include <math.h>
#include <ctype.h>

void PrintReverse(int n)
{
if (n < 0)
{
printf("-");
n = -n;
}
do
{
printf("%d ",n%10);
n /= 10;
} while (n != 0);

printf("\n");

}

int main()
{
PrintReverse(123456789);
PrintReverse(0);
PrintReverse(-123456789);
return 0;
}

运行分析:
在这里插入图片描述
//(3)顺序输出十进制n的每一个数字

//代码如下:
#include <stdio.h>
#include <math.h>
#include <ctype.h>

int GetFigures(int n)
{
if(n==0)
return 1;
int count = 0;
while(n!=0)
{
n/=10;
count++;
}
return count;
}

void PrintOrder(int n)
{
int count = GetFigures(n);
int power = (int)pow(10.0,(count-1));

do
{
	printf("%d ",n/power);
	n %= power;
	power /= 10;
} while (n!=0);
printf("\n");

}

int main()
{
PrintOrder(123456789);
PrintOrder(0);
PrintOrder(-123456789);
return 0;
}

运行分析:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值