2013-07-15 38 views
2

我已经为因子分解一个Delphi程序,它看起来像这样:输出错误的因子分解

enter image description here

40是多少,当你点击“Scomponi”你有正确的输出(2^3 * 5 = 40)。我做了一个C程序应该做同样的事情,但我有这样的输出:

enter image description here

正如你可以看到,右边的数字是正确的(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;  
} 

我该如何解决这个问题?

+1

我想这个'printf(“%d”,a);'是多余的。 – JeffRSon

回答

8
else 
    { 
    b++;  
    } 
    printf("%d", a); 
    } 

最后的printf不应该在这里。去掉它。

作为附带说明:

ablong类型。

因此,而不是:你必须使用

scanf("%d", &a); 

scanf("%ld", &a); 

同为printf,使用%ld转换规范,而不是%d

+0

谢谢,它适用于我:) –

+1

@ DK64不客气! – ouah