逆序输出数字

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

1.求出它是几位数;

2.分别输出每一位数字;

3.按逆序输出个位数字,例如原数为321,应输出123.

#include<stdio.h>
//计算m的n次方
int sqrt(int m,int n){
int i = 1;
int a = m;
if(m == 0){
return 0;
}
if(n==0){
return 1;
}
while(i < n ){
m *= a;
i++;
}
return m;
}

//判断整数n是几位数
int test_01(int n){
int i = 0;
do{
i++;
n /= 10;
}while(n != 0);
         return i;
}


//逆序输出n的每一位数字
void test_02(int n){
while(n != 0){
int a = n % 10;
        n /= 10;
printf("%d ",a);
}
printf("\n");
}


//n中有几个 1 ?
void test_03(int n){
int i = 0;
while(n != 0){
int a = n%10;
if(a == 1){
i++;
}
n /= 10;
}
printf("这个数字有 %d 个 1\n",i);
}


//正序输出n的每一位数字
void test_04(int n){
while( n != 0){
int temp = sqrt(10,test_01(n)-1);
int m = n/temp;
n %= temp;  
printf("%d ",m);
}
printf("\n");


}


int main(){
printf("%d\n",test_01(3521));
printf("%d\n",test_01(618951));
printf("%d\n",test_01(118151));
printf("%d\n",test_01(0));


printf("\n");
test_02(3521);
test_02(618951);
test_02(118151);


printf("\n");
test_03(3521);
test_03(618951);
test_03(118151);


printf("\n");
test_04(3521);
test_04(618951);
test_04(118151);


printf("\n");
printf("%d\n",sqrt(3,2));
printf("%d\n",sqrt(2,3));
printf("%d\n",sqrt(5,0));
printf("%d\n",sqrt(3,1));
printf("%d\n",sqrt(0,2));
printf("%d\n",sqrt(2,0));
printf("%d\n",sqrt(0,0));
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值