0
点赞
收藏
分享

微信扫一扫

实验4-2-8 输出整数各位数字 (15分)


本题要求编写程序,对输入的一个整数,从高位开始逐位分割并输出它的各位数字。

输入格式:
输入在一行中给出一个长整型范围内的非负整数。

输出格式:
从高位开始逐位输出该整数的各位数字,每个数字后面有一个空格。

输入样例:
123456
输出样例:
1 2 3 4 5 6

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
long n, i, cnt = 0, temp;
scanf("%ld", &n);
if ( n == 0){
printf("0 ");
return 0;
}
temp = n;
while(temp){
temp /= 10;
cnt++;
}
temp = n;
for ( i = cnt; i > 0; i--){
printf("%ld ", (long)(temp/(pow(10, i - 1))));
temp -= (long)(temp/(pow(10, i - 1)))*(pow(10, i - 1));
}

return 0;
}

实验4-2-8 输出整数各位数字 (15分)_#include


举报

相关推荐

0 条评论