Program to find sum of N is less than x and N+1 is greater

I am trying to write a program that will find an integer N that the sum of 1+2+3...N is less than 31096 and N+1 is greater than 31096.

I wrote a program yesterday that will add the sum of all numbers up to an integer given by the user. But I am struggling to write one for this current problem. Below is the code from the program I already wrote. I thought it would be a good starting point to figure out the current one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
       using namespace std;

       int main()
       {
           int num, sum=0, i;
           
           cout<<"Enter any number : ";
           cin>>num;
           
           {for(i=0; i<=num; i++)
           sum+=i;
           }
           
           cout<<"Sum of all numbers up to "<<num<<" is "<<sum << endl;
           
           system("pause");
       return 0;
           }


any help with this will be greatly appreciated. I am new to C++ and programming in general and I'm really struggling.
So:

1+2+3+4+5...+N < 31096 && N+1 > 31096

Unless I'm missing something, it doesn't seem possible that the above statement would ever be true without skipping numbers in your number sequence.

Could you go into a little more detail?
Sorry about that, it's supposed to be:

(1 + 2 + 3... + N < 31096) && (1 + 2 + 3.. + N + (N + 1) > 31096)
Last edited on
I think this will give you a good idea of what you want. If you have any questions, ask away (or if I made a false assumption about your question, let me know that too :P )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
	int n = 0;
	int total = 0;

	for (int i = 1; i < 10000; i++)
	{
		n = i + 1;
		total += i;

		if ( (total + n + 1 < 31096) && (total + (n * 2) + 1 > 31096) )
			cout << n << endl;
	}

	cout << "Done.\n";
	cin.ignore();
	return 0;
}
I think there's something really off about this part:
(total + (n * 2) + 1 > 31096)

I think the loop can be even simpler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	int i, total = 0, limit;
	cout << "Enter an integer: ";
	cin >> limit;
	getchar(); //eat the \n
	for (i = 0; total < limit; i++)
	{
		total += i;
	}
	cout << "\nThe sum of all positive integers up to and including:\n";
	cout << "\n" << i - 1 << " is " << total - i;
	cout << "\n" << i << " is " << total << "\n\n";
	cin.ignore();
	return 0;
}
I might be reading this wrong. How can the sum of all of the numbers be less than the number? (The sum must be less than 31096 but the number is greater than 31096)
Last edited on
@fg109

Read his entire post again, and suddenly my program will make sense :)
if the n + 1 is greater than 31096, then n can only be a minimum of 31096. So are you trying to determine the last number in the sequence before the total of the sequence becomes 31096?
He should have edited his original post, but here's the actual test expression:

(1 + 2 + 3... + N < 31096) && (1 + 2 + 3.. + N + (N + 1) > 31096)
@Phil123

OK, I see it now... It's just (total + (n + 1) + (n + 2) > 31096) if I multiply it out. I still think it can be simplified.

The code I posted before was wrong (I messed up on the loop logic...), but the idea is there. Corrected version:

1
2
3
4
5
6
for (n = 0; total + n < 31096; n++)
{
	total += n;
}

cout << n - 1 << endl;
Topic archived. No new replies allowed.