求教:为什么我着个程序计算不了5位数?
代码如下:#include<stdio.h>
main()
{
int num,fannum,i,n,m;
printf("This program reverses the digits in an interger.\n");
printf("Enter a positive integer:");
scanf("%d",&num);
n=num;
i=0;
m=1;
while (n>0) {
n/=10;
i++;
m*=10;
}
fannum=0;
m/=10;
while (num>0) {
n=num%10;
fannum+=n*m;
m/=10;
num/=10;
i--;
}
printf("The reversed number is %d\n",fannum);
}
在TC2.0下运行,输入5位数就会输出负数,搞不明白啊。
搜索更多相关的解决方案:
位数
----------------解决方案--------------------------------------------------------
#include<stdio.h>
main()
{
long num,fannum,i,n,m;
printf("This program reverses the digits in an interger.\n");
printf("Enter a positive integer:");
scanf("%ld",&num);
n=num;
i=0;
m=1;
while (n>0) {
n/=10;
i++;
m*=10;
}
fannum=0;
m/=10;
while (num>0) {
n=num%10;
fannum+=n*m;
m/=10;
num/=10;
i--;
}
printf("The reversed number is %ld\n",fannum);
}
----------------解决方案--------------------------------------------------------
谢谢,变量的类型弄错了
----------------解决方案--------------------------------------------------------