数字之和 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7463507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:59:23  来源:igfitidea点击:

Sum of Numbers C++

c++for-loopsum

提问by soniccool

I am supposed to write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50.

我应该编写一个程序,要求用户输入一个正整数值。程序应该使用循环来获取从 1 到输入的数字的所有整数的总和。例如,如果用户输入 50,循环将找到 1、2、3、4、... 50 的总和。

But for some reason it is not working, i am having trouble with my for loops but this is what i have down so far.

但由于某种原因它不起作用,我的 for 循环有问题,但这就是我到目前为止所遇到的问题。

#include <iostream>
using namespace std;

int main()
{
    int positiveInteger;
    int startingNumber = 1;
    int i = 0;

    cout << "Please input an integer up to 100." << endl;

    cin >> positiveInteger;

    for (int i=0; i < positiveInteger; i++)
    {
        i = startingNumber + 1;
        cout << i;
    }

    return 0;

}

I am just at a loss right now why it isn't working properly.

我现在不知道为什么它不能正常工作。

回答by Ernest Friedman-Hill

The loop is great; it's what's inside the loop that's wrong. You need a variable named sum, and at each step, add i+1to sum. At the end of the loop, sumwill have the right value, so print it.

循环很棒;这是循环内部的错误。您需要一个名为 的变量sum,并在每一步添加i+1sum。在循环结束时,sum将具有正确的值,因此打印它。

回答by xxcv

try this:

尝试这个:

#include <iostream>
using namespace std;

int main()
{
    int positiveInteger;
    int startingNumber = 1;

    cout << "Please input an integer upto 100." << endl;

    cin >> positiveInteger;

    int result = 0;
    for (int i=startingNumber; i <= positiveInteger; i++)
    {
        result += i;
        cout << result;
    }

    cout << result;

    return 0;

}

回答by zackery.fix

I have the following formula that works without loops. I discovered it while trying to find a formula for factorials:

我有以下公式可以在没有循环的情况下工作。我在试图找到阶乘公式时发现了它:

#include <iostream>
using namespace std;

int main() {
    unsigned int positiveInteger;
    cout << "Please input an integer up to 100." << endl;
    cin >> positiveInteger;

    cout << (positiveInteger * (positiveInteger + 1)) / 2;
    return 0;
}

回答by Jiri Kriz

You can try:

你可以试试:

int sum = startingNumber; 
for (int i=0; i < positiveInteger; i++) {     
    sum += i;
}
cout << sum;

But much easier is to note that the sum 1+2+...+n = n*(n+1) / 2, so you do not need a loop at all, just use the formula n*(n+1)/2.

但是要注意 sum 容易得多1+2+...+n = n*(n+1) / 2,因此您根本不需要循环,只需使用公式即可n*(n+1)/2

回答by Roland Illig

First, you have twovariables of the same name i. This calls for confusion.

首先,您有两个同名的变量i。这需要混淆。

Second, you should declare a variable called sum, which is initially zero. Then, in a loop, you should add to it the numbers from 1 upto and including positiveInteger. After that, you should output the sum.

其次,您应该声明一个名为 的变量sum,该变量最初为零。然后,在循环中,您应该向其中添加从 1 到包括 的数字positiveInteger。之后,您应该输出sum.

回答by rohit89

You are just updating the value of iin the loop. The value of ishould also be added each time.

您只是i在循环中更新 的值。的值i也应该每次添加。

It is never a good idea to update the value of iinside the forloop. The forloop index should only be used as a counter. In your case, changing the value of iinside the loop will cause all sorts of confusion.

ifor循环内部更新 的值从来都不是一个好主意。该for循环指数只应作为计数器。在您的情况下,更改i循环内的值会导致各种混乱。

Create variable totalthat holds the sum of the numbers up to i.

创建一个变量total来保存直到 的数字总和i

So

所以

 for (int i = 0; i < positiveInteger; i++)
        total += i;

回答by Polaris878

mystycs, you are using the variable ito control your loop, howeveryou are editing the value of iwithin the loop:

mystycs,您正在使用变量i来控制循环,但是您正在编辑i循环内的值:

for (int i=0; i < positiveInteger; i++)
{
    i = startingNumber + 1;
    cout << i;
}

Try this instead:

试试这个:

int sum = 0;

for (int i=0; i < positiveInteger; i++)
{
    sum = sum + i;
    cout << sum << " " << i;
}

回答by Tony The Lion

int result = 0;


 for (int i=0; i < positiveInteger; i++)
    {
        result = startingNumber + 1;
        cout << result;
    }