Write a program that finds an integer N such that the sum 1
Write a program that finds an integer N such that the sum 1 + 2 + 3 + ... + N is less than the value 31096 and the sum 1 + 2 + 3 + ... + N + (N + 1) is greater than 31096. Print out the value of N. You only need to output N, not N+1.
c++ format, preferably codeblocks or in notepad
Solution
#include <iostream>
using namespace std;
int main()
{
int sum=0, n=0;
int i=1;
for(;;){
sum = sum + i;
if(sum >= 31096 ){
n = i-1;
break;
}
i++;
}
cout<<\"The value of N is \"<<n<<endl;
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
The value of N is 248
