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>
usingnamespace 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.
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 )
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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)
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?