Write a program that asks the user to enter two numbers, obtains the two numbers
from the user, and prints the sum, product, difference, and quotient of the two
numbers.
#include <iostream>
using namespace std;
int main() {
float n1, n2;
cout<<"Enter n1: ";
cin>> n1;
if(!cin){ // validation
cout<<"Invalid input."<<endl;
cin.clear(); // clears the error flag
exit(1);
}
cout<<"Enter n2: ";
cin>> n2;
if(!cin){ // validation
cout<<"Invalid input."<<endl;
cin.clear(); // clears the error flag
exit(1);
}
cout<<"SUM: "<<n1+n2<<endl;
cout<<"PRODOCT: "<<n1*n2<<endl;
if(n1>n2)
cout<<"DIFFERENCE: "<<n1-n2<<endl;
else
cout<<"DIFFERENCE: "<<n2-n1<<endl;
if(n2!=0)
cout<<"QUOTIENT: "<<n1/n2<<endl;
cout<<endl;
return 0;
}
Comments
Leave a comment