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.

Dani AI

Generated

A compact, constant-time alternative to the loop solutions already shown by and the input-order suggestion from is to use the arithmetic series formula. Let a be the smaller input and b the larger; the count of integers strictly between them is n = b - a - 1. If n <= 0 the sum is 0. Otherwise the sum is

S = n * (a + b) / 2

This works for negative numbers as well and runs in O(1) time instead of iterating through every value.

Example implementation (keeps intermediate values in 64-bit and divides before multiplying to reduce overflow risk):

#include <iostream>
#include <algorithm>

int main() {
    long long x, y;
    if (!(std::cin >> x >> y)) return 0;
    long long a = std::min(x, y);
    long long b = std::max(x, y);
    long long n = b - a - 1;
    if (n <= 0) { std::cout << 0 << '\n'; return 0; }
    long long s = a + b;
    long long sum;
    if ((n & 1) == 0) sum = (n/2) * s;
    else sum = n * (s/2);
    std::cout << sum << '\n';
    return 0;
}

Notes and pitfalls: adjacent or equal inputs produce n <= 0 so return 0; the formula yields an integer always, so dividing one factor by 2 first is safe. For very large ranges the product can still overflow 64-bit—use __int128 (GCC/Clang) or Boost.Multiprecision if inputs may exceed roughly 1e9. This approach is concise, avoids loops, and is numerically stable when the appropriate integer type is chosen.

Recommended Answers

All 7 Replies

>>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.