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.
#include <iostream>
#include <conio.h>
usingnamespace 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 bif(k == i)
cout << "Sum from " << a << " to " << b << " is " << i << endl;
b++;
}while(b < i / 2);
}
_getch();
return 0;
}