so i stumbled upon a few 'challenge questions' and decided to have a go. it should be my level. so anyways
the question asked me to produce a code that makes me-
input 2 numbers.
the numbers get added ASWELL as the un numbers inbetween BUT excluding the numbers u input.
then display the result.

ie. if i input 5 and 2. the code should go ( 4+3)
if i input -4 and 5 the code should go(-3 + -2 + -1 + 0 + 1 + 2 + 3 + 4)
if i input 11 and 3 the code should go (10+9+8+7+6+5+4) *notice how it excludes 11 and 3?
it ignores ur input but adds numbers in between.
heres what i got so far

#include <iostream>
using namespace std;
int main( )
{
    int total = 0;
    int X, Y;
    cout<<"please input 1st number ";
    cin>>X;
    cout<<"please input 2nd number ";
    cin>>Y;

while(X>Y)
{total=X+Y+total;
X=X-1;
Y=Y+1;
}

cout<<"total of all integers in between is "<<total;
system("pause");
return 0;
}

but..this only works if X is greater than Y
what should i do to make it Y>X work as well?
ive tried using the 'else if' and 'switch case' but i seem to fail at that.
thanks in advance.

>>what should i do to make it Y>X work as well

Simple -- just swap them.

int sum = 0;
if( x > y)
{
   int tmp = x;
   X = Y;
   Y = TMP;
}

for(x = x + 1; x < y; x++)
{
  sun = sum + x;
}

thnx for responding
will that make it both work within one code? and if so how would i insert it?

sorry but i was under the impression you are suppsoed to do it this way:

if
1.X>Y
while
display result

else if
2.Y>X
while loop
display result

else
cout<<"invalid input";
end of code.

i had difficulty attempting this. i will show the code if asked but im still looking into ancient dragon's code

#include <iostream.h>

using namespace std;
int main( )
{
    int total = 0;
    int X, Y;
    cout<<"please input 1st number ";
    cin>>X;
    cout<<"please input 2nd number ";
    cin>>Y;
if(X>Y)
{
Y++;
do
{
total+=Y;
Y++;
}while(X>Y);
}
else if(Y>X)
{
X++;
do
{
total+=X;
X++;
}while(Y>X);
}
cout<<"total of all integers in between is "<<total;

return 0;
}

that works, thanks very much. but to get this finally solved, one last step could you please explain what exactly this part does, (particularly 'total+' do??) i have come across this
before but i don't truly understand.

{
Y++;
do
{
total+=Y;
Y++;
}while(X>Y);
}

total+=y is the same as total = total + y. Just a shorthand way of saying it and either is perfectly acceptable.

{
Y++;
do
{
total+=Y;
Y++;
}while(X>Y);
}

This block is executed when Y<X
working of code
increase Y by 1 1st, then your enter into while loop
working of loop
add Y to Total while Y<X

and total+=y is the same as total=total+y. Just a shorthand operator is used both are fine no problem at all.
Best Of Luck.

thanks very much. the code is sucessful and i learnt something new today.

Be a part of the DaniWeb community

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