c语言用循环的方法,把输入的一个整数,倒序输出

如题所述

思路:可以利用while循环依次对其进行对10取余输出并除10操作直到其为0为止,输出的结果就是该整数的倒序。

参考代码:

#include <stdio.h>
int main()
{
int i;
scanf("%d",&i);
while(i)
{
    printf("%d ",i%10);
    i/=10;
}
return 0; 
}
/*
输出:
12345
5 4 3 2 1
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-21

取余加除法就可以了

#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
while(a)
{
printf("%d",a%10);
a/=10;
}
return 0;
}

本回答被网友采纳
第2个回答  2015-07-21
#include<stdio.h>
#include<string.h>
main()
{
char str[100];
int n;
printf("please input a number:");
gets(str);
n=strlen(str);
for(;n>=0;n--){
printf("%c",str[n])
};
}

关键你没说多少位数字,所以最好转换成字符串,这样处理起来比较方便
相似回答