THE QUESTION

with a positive integer s read from the keyboard, find all sequences of two or more consecutive integers whose sum is = to s.... for example.... if user enters 15

result should show

1 2 3 4 5....
4 5 6...
7 8....

MY TRY

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  int num, total = 0;
  
    cout << "Enter a digit to work it out: "  ;
    cin >> num;
    cout << endl;
  
  for (int n = 1; n <= num; n++)
  {
    for (int i = n; i <= num; i++)
    {
     if (i <= (num / 2)+ 1)
     {
     total+= n;
     cout << i << " ";
     }
    }
    cout << endl;
  }  
  cout << endl << endl;     
  system("PAUSE");	
  return 0;
}

MY OUTPUT
if i enter 15 is


1 2 3 4 5 6 7 8
2 3 4 5 6 7 8
3 4 5 6 7 8
4 5 6 7 8
5 6 7 8
6 7 8
7 8
8

PLZ HELP...i dnt know what to do next...im new to this.... :/

mvmalderen commented: For using code tags on first post :) +6

Recommended Answers

#include <stdlib.h> is old style C++, if your compiler supports it you should consider using the new-style header file: #include <cstdlib> BTW, Read the third remark in my signature :P

Jump to Post

Now I have really got mad upon iamthwee. What was the cause of infraction for my post #4. Just because I gave an infraction to him, he gave me an infraction. Isn't it kiddish?
iamthwee, I was not spoon feeding, but you were. It is noway to behave in this …

Jump to Post

All 7 Replies

#include <stdlib.h> is old style C++, if your compiler supports it you should consider using the new-style header file: #include <cstdlib> BTW, Read the third remark in my signature :P

At some point, you need to check to see if the sequence adds up to the value in question. Consider something like this as a starting point.

void foo(int value, int start)
{
   int i, sum = 0, match = 0;
   for ( i = start; i <= value / 2 + 1; ++i )
   {
      sum += i;
      if ( sum == value )
      {
         match = 1;
         break;
      }
   }
   // ...
}

A little mathematics can do the magic. The sum of consecutive integers from [tex]n_1[/tex]
to [tex]n_2[/tex] (including both [tex]n_1[/tex] and [tex]n_2[/tex])
is given by:

[tex]sum=n_2(n_2+1)-n_1(n_1-1) \over 2[/tex]

(If you need a proof(which is quite trivial), let me know)


Hence the problem now remains to run a nested loop with iterators [tex]n_2[/tex] and [tex]n_1[/tex] such that [tex]1<n_1<n_2<s[/tex] and check if [TEX]sum=s[/TEX]

commented: stop the spoon feeding chicken! -4
commented: Seemed like a good answer to me :) +32
#include <string>
#include <iostream>

using namespace std;

class Fools
{
public:
   void bar ( void )
   {
      int num;
      cout << "Enter a num:";
      cin >> num;

      for ( int i = 1; i < num; i++ )
      {
         for ( int j = 1; j < num; j++ )
         {
            if ( ( i* ( i + 1 ) - j* ( j - 1 ) ) / 2 == num )
            {
               printSeq ( j, i );
               cout << "\n";
            }
         }
      }
   }

   void  printSeq ( int i, int j )
   {
      for ( int a = i; a <= j; a++ )
      {
         cout << a << " ";
      }
      cout << " ...";
   }
};

int main ( void )
{
   Fools gold;
   gold.bar();

   cin.get();
   cin.get();
}

[edit] looks like I was beaten

commented: Won't you stop doing your spoon feeding? -1
commented: You really shouldn't give away the whole code... :P +0
commented: In more ways than 1 -6
commented: orange spots >_< begone! +6

thanx guys... i solved it another way.... heres the code i used.... hope it helps any1 stuck with the same prob

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main(void)
{
    int varnum, varcount = 1, varinc = 2, seq[100], varinc2 = 1, varinc3 = 2, vartotal = 0;
    bool flag = false;
    string varanswer;
    do
    {
    flag = false;
    do
    {
        cout << "Please enter a positive number to display the sequences of consecutive \nintergers who's sum is equal to that number.\n";
        cin >> varnum;
    }
    while(varnum < 1);

    do
    {
    varinc = varinc3;
    varinc2 = 1;
    varcount = varinc3-1;
    vartotal = 0;
    while(varcount <= varnum)
    {
        seq[varinc2] = varinc - 1;
        //cout << "Value " << varinc2 << " is: " << varcount << " and the array is " << seq[varinc2] << "\n";
        varcount = varcount + varinc;
        varinc++;
        varinc2++;
    }
    //cout << "\n";

    for(int counter2 = 1; counter2 <= varinc2 - 1; counter2++)
    {
        vartotal += seq[counter2];
    }
    //cout << "This is the total " << vartotal << "\n";

    if(vartotal == varnum)
    {
    for(int counter = 1; counter <= varinc2 - 1; counter++)
    {
        cout << seq[counter] << " ";
        flag = true;
    }
    cout << "\n";
    }
    varinc3++;
    }
    while(varinc3 <= (varnum + 1) / 2); 
    if(flag == false)
    {
        cout << "This number has no sequences of consecutive intergers who's sum is equal to it.\n";
    }
    cout << "\n";
    cout << "Do you want to choose another number?(yes/no)\n";
    cin >> varanswer;
    system("cls");
    }
    while(varanswer == "yes");
    system("pause");
    return 0;
}

Why don't you post using code tags?
Is that maybe too difficult?

Now I have really got mad upon iamthwee. What was the cause of infraction for my post #4. Just because I gave an infraction to him, he gave me an infraction. Isn't it kiddish?
iamthwee, I was not spoon feeding, but you were. It is noway to behave in this forum.
I will surely get to some moderator about your 'spoon feeding' behavior.
One thing is that a man makes a mistake. The second thing that he doesn't realize it. The worst thing is that he don't want to.

You are really having some superiority complex. As you keep showing it with your pathetic coding style. Now who would have been though that a simple procedural problem would have included OOP?

The worst part is that you are featured poster. Does Daniweb has no criteria to give that tittle?

I don't want keep this thread dirty. Any agreement or disagreement from anyone else iamthwee are subjected to the reputation counter of this post. Any further discussion should be directed to my PM box with the reference of this thread.

Thank You

commented: I agree :) +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.