Answer to Question #340330 in C++ for Nia

Question #340330

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.

1
Expert's answer
2022-05-12T08:21:22-0400
#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;
}

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
APPROVED BY CLIENTS