Find all the sequences of integers that the sum equal the number s

With a positive integer s read from the keyboard, find all sequences of 2 or more consecutive integers whole sum is equal to s.For example if s is 15,there are 3 solutions:
1 2 3 4 5
4 5 6
7 8
I have code like this:
//find all the sequences of 2 or more consecutive numbers whole sum is equal to s
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
cout<<"Please enter the number ";
int s;
int sum=0;
cin>>s;
cout<s;
int x;
for(int a=1;a<s;a++)
{
x=a;
while(sum<s)
{
x++;
sum+=x;
}
cout<<"The sum is "<<sum<<endl;
if(sum==s)
for(int i=x;i>0;i--)
{
cout<<i;
}
}
getch();
return 0;
}
But it seems the compiler doesn't run the while loop and so on.After i entered the s , nothing happen.
Can anyone help me please.the idea is this,but i cannot find the mistake to fix.

// try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <conio.h>
using namespace std;

int main() {

	int i;
	cout << "Input int: ";
	cin >> i;

	if(i < 3)
		return 0;

	for(int a(1); a <= (i / 2 + 1); a++) {
		int b(a + 1);
		do {
			int k;
			k = ((a + b) / 2.0 * (b - a + 1)); // count the sum from a to b
			if(k == i)
				cout << "Sum from " << a << " to " << b << " is " << i << endl;
			b++;
		}while(b < i / 2);
	}

	_getch();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.