用C语言编程中输入一个正整数,把数字前后颠倒并输入颠倒后的结果,怎样操作?

Python0239

用C语言编程中输入一个正整数,把数字前后颠倒并输入颠倒后的结果,怎样操作?,第1张

用字符串处理很简单

#include <stdio.h>

#include <string.h>

void main ()

{

int n,i

char s[20]

scanf("%d", &n)

sprintf(s,"%d", n)

printf("%d\n",strlen(s))

for(i=strlen(s)-1i>=0i--){

printf("%c",s[i])

}

printf("\n")

}

如果要用循环也可以的。

补充:

#include<stdio.h>

void main()

{

long x

int temp=0,num=0

printf("请输入一个整数:\n")

scanf("%ld",&x)

printf("它的每一位数字是:\n")

while(x>0)

{

printf("%3d",x%10)

temp=temp*10+x%10

x=x/10

num++

}

printf("\n它是一个%d位数.\n",num)

printf("它的逆序是:%d\n",temp)

}

思路:逆序输出一个整数可以对其除10直到其为0为止,并输出其对10取余,最后的结果就是这个整数的逆序。

参考代码:

#include

int main()

{

int n

scanf("%d",&n)

while(n)

{

printf("%d ",n%10)

n/=10

}

return 0

}

/*

输出:

12345

5 4 3 2 1

*/