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; }

9th Nov 2016, 10:30 AM
Catalin Dervesteanu
Catalin Dervesteanu - avatar
8 Respuestas
+ 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
9th Nov 2016, 10:35 AM
Megatron
Megatron - avatar
+ 2
Well 17 cannot be expressed as sum of consecutive numbers. Entered 18,21,33 and got correct output.
9th Nov 2016, 11:08 AM
Megatron
Megatron - avatar
+ 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.
9th Nov 2016, 11:29 AM
Megatron
Megatron - avatar
+ 2
I tried by copying your program input=17 output is there 8 9
9th Nov 2016, 12:42 PM
Megatron
Megatron - avatar
0
for 17 it shows nothing, same for greater odd numbers
9th Nov 2016, 10:41 AM
Catalin Dervesteanu
Catalin Dervesteanu - avatar
0
17=8+9?it shows no output
9th Nov 2016, 11:20 AM
Catalin Dervesteanu
Catalin Dervesteanu - avatar
0
tried without the endl, still not working
9th Nov 2016, 12:23 PM
Catalin Dervesteanu
Catalin Dervesteanu - avatar
0
I finally saw it, thaks for help, I need to pay more attention next time
9th Nov 2016, 3:08 PM
Catalin Dervesteanu
Catalin Dervesteanu - avatar