开发者

Sum of Numbers C++

开发者 https://www.devze.com 2023-04-05 12:36 出处:网络
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

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.

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.

#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 = 开发者_如何学JAVAstartingNumber + 1;
        cout << i;
    }

    return 0;

}

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


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+1 to sum. At the end of the loop, sum will have the right value, so print it.


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;

}


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;
}


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.


mystycs, you are using the variable i to control your loop, however you are editing the value of i within the loop:

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;
}


int result = 0;


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


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

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.


You are just updating the value of i in the loop. The value of i should also be added each time.

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

Create variable total that holds the sum of the numbers up to i.

So

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

精彩评论

暂无评论...
验证码 换一张
取 消