這是我的倒數計劃。但是當我以5位數字作爲輸入時,有時候答案是正確的,肯定的,有時候是負數。顛倒5位數字給出負數
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
clrscr();
int a,b,c;
b=0;
printf("Enter the no");
scanf("%d",&a);
c=a;
while(a>0)
{
b=(b*10)+(a%10);
a=a/10;
}
printf("\noriginal no %d",c);
printf("\nreversed no is %d",b);
getch();
return 0;
}
如果輸入:12111
輸出:11121
輸入:22333
輸出:-32214
整數的限制是從-32768到32767,那麼爲什麼是否定的答案? 我試過使用很長,但我得到了我的反向數字爲0.
如果你的'int'是2個字節,那麼你有一個特別舊的C實現。 – Bathsheba
爲什麼要在數學上「倒轉」數字的艱難路線?爲什麼不只是逐個字符地反轉? – dreamlax
@Bathsheba看看前幾行。對於MS-DOS,這很可能是Turbo C ...。 –