階乗分解用の Delphi プログラムを作成しましたが、次のようになります。
40 は数字で、「Scomponi」をクリックすると正しい出力が得られます (2^3 * 5 = 40)。同じことを行うCプログラムを作成しましたが、次の出力があります。
ご覧のとおり、右側の数値は正しい (2^3 * 5) ですが、左側の数値は正しくありません。これは私が書いたコードです:
int main()
{
long a,b=2;
printf("------------------------------------------------ \n \n");
printf("Inserisci il numero di cui vuoi la scomposizione \n"); //input number (it's the 40 of the example)
scanf("%d", &a);
printf("\n------------------------------------------------ \n\n");
printf("Scomposizione: \n \n"); //Decomposition
while(a>1)
{
if(a%b == 0)
{
printf("%d \t \t | %d \n",a,b);
a=a/b;
}
else
{
b++;
}
printf("%d", a);
}
printf("\n------------------------------------------------ \n\n");
getch();
return 0;
}
この問題を解決するにはどうすればよいですか?