0
Trying to make this code show all variants of writing a number as sum of consecutive numbers,but most of the times shows nothing
#include <iostream> using namespace std; int main() { int n,i,j,s,k; cin>>n; for(i=1;i<=n/2;i++) { j=i; s=0; while(s<n) { s+=j; j++; } if (s==n) for (k=i;k<=j-1;k++) { cout <<k<<" "; } cout <<endl; } return 0; }
8 Respostas
+ 2
Tried the code in sololearn c++ compiler(app) It works fine.
I entered 15. Output was
1 2 3 4 5
4 5 6
7 8
+ 2
Well 17 cannot be expressed as sum of consecutive numbers.
Entered 18,21,33 and got correct output.
+ 2
17 cannot be expressed as sum of consecutive numbers.
Sorry for that. didn't paid attention.
scroll down in output screen.
The last
cout<<endl;
repeats so many times that
it takes the output outside the visible region.
+ 2
I tried by copying your program
input=17
output is there 8 9
0
for 17 it shows nothing, same for greater odd numbers
0
17=8+9?it shows no output
0
tried without the endl, still not working
0
I finally saw it, thaks for help, I need to pay more attention next time