Answer to Question #152766 in C++ for Noman khan

Question #152766
Write a program which takes as input two integers and displays the sum of all the integers that lie between those two numbers. E.g., if x is 4 and y is 8, the answer would be 18 and if x is 8 and y is 4, the answer would be 18.
1
Expert's answer
2020-12-26T03:06:15-0500
#include <iostream>


using namespace std;


int main()
{
    int a,b, sum=0;
    cout<<"Enter a: ";  cin>>a;
    cout<<"Enter b: ";  cin>>b;
    if(a>b){//swap a & b
        a+=b;
        b= a-b;
        a=a-b;
    }
    for(int i=a+1;i<b;i++)
        sum+=i;
    cout<<"Sum: "<<sum<<endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog