Problems
. Using the if...else if...else if......else, write a C++
program that
- Asks the user to input two integers.
- If both of them are greater than zero, then output their sum.
- If only one integer is greater than zero, then output their difference.
- Otherwise, output a message "your two number <0".
Hi there. The following is the answer to your question.
#include <bits/stdc++.h>
using namespace std;
int main(){
int n1, n2;
cout<<"Enter two integers"<<endl;
cin>>n1>>n2;
if(n1>0 && n2>0)
cout<<(n1+n2)<<endl;
else if(n1>0 || n2>0)
{
if(n1>0) //make sure output is positive
cout<<(n1-n2)<<endl;
else
cout<<(n2-n1)<<endl;
/* For subtracting second from first
cout<<(n1-n2)<<endl;
*/
}
else
cout<<"your two number <0"<<endl;
}
Note : Since difference is not commutative and it was not mentioned which number has to be subtracted from which, I've considered the case where smaller is subtracted from larger. In case you want to subtract second from first, uncomment the line of code.
Get Answers For Free
Most questions answered within 1 hours.