C语言求逆序输出各位数字

C语言求逆序输出各位数字,刚学C语言不太懂,请大家帮个忙!

思路:逆序输出一个整数,最简单的方法就是利用字符数组接收该整数,求出数组长度,依次从数组的高位到低位输出即可。

参考代码:

#include <stdio.h>
#include <string.h> 
int main ()
{
char a[100];
int i,n;
gets(a);//输入整数字符数组接收 
n=strlen(a);//求数组长度 
for(i=n-1;i>=0;i--)//从数组高位到低位输出 
printf("%c",a[i]);
return 0; 
}
/*
运行结果:
12345
54321
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-02
最简单的答案:

#include<stdio.h>
main()
{
int a, b = 0;
scanf("%d",&a);
while(a != 0)
{
b = b*10 + a%10;
a /= 10;
}
printf("%d\n", b);
}

//输入原数,就会输出答案。这样不需要字符串,而且负数也同样可以实现
第2个回答  2010-05-05
#include<stdio.h>
void main()
{
int a,b=0;
scanf("%d",&a);
while(a!=0)
{
b=b*10+a%10;
a=a/10;
}
printf("%d\n", b);
}
第3个回答  推荐于2017-10-15
#include <stdio.h>
#include "string.h"

void main()
{
char str[100];
int s,i,n;
printf("input a number:");
scanf("%d",&s);
for(i=0;s>0;i++)
{
n=s%10;
s=(s-n)/10;
str[i]=n+48;
}
str[i]='\0';
puts(str);
}本回答被提问者采纳
相似回答