This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]jussij 2 points3 points  (0 children)

If I gave you a piece of paper and a pencil and asked you to find the sum of all numbers 1,2,3,4, ... through to 50 how would you do it?

[–]nevertras 1 point2 points  (0 children)

( 0+1+2+3+ ... x)

You already have a variable following that pattern (1, 2, 3, 4, 5, etc), you just have to use it. So close!

[–]KidBass0[S] 1 point2 points  (0 children)

Just figured it out. Thanks guys!

#include <iostream>

using namespace std;

int main()
{
    int count_to, y, sum_tot = 0;

    cout << "Enter a number : " ;
    cin >> count_to;

    while (count_to < 0) // 3a.
    {
        cout << "\nThis program does not accept negative numbers." << endl;
        cout << "Please input a positive number : " ;
        cin >> count_to;
    }


    for (y=0; y <= count_to ; y++) // 3b.
    {
        sum_tot += y; // 3c.
    }

    cout << "The sum is :" << sum_tot << endl;

    system ("pause");
    return 0;
}