簡體   English   中英

為什么我在C ++中不斷得到錯誤的輸出

[英]Why do I keep getting the wrong output in c++

int main()
{
int n, i, sum;

cout << "Enter a value for n: ";
cin >> n;
sum = 0;

    i = 1;
    do
{
    sum += i * i;
    i++;
}while (i <= n);
cout << "The sum of the first " << n
    << " numbers is " << sum << endl;

return 0;
}

每當我運行此代碼並使用4作為示例時,輸出結果為30。4的階乘為24時

sum+=i*i 

->前n平方的和。

階乘是

prod=prod*i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM