Write a program that asks the user to enter two decimal numbers.The program should calculate and display the product and quotient of the two numbers. Use a function Product() to calculate the product. , The function should have two arguments which are two numbers that the user inputs. The function should return the product of the numbers. Use a function Quotient() that has two arguments, which are the numbers input by the user. The function should return the quotient of the first number divided by the second. If the second number is zero (recall that division by zero is not allowed). display an error message and exit the program.
1
Expert's answer
2014-09-24T13:36:34-0400
#include<iostream> #include<string>
using namespace std; //function Product() to calculate the product. //The function should have two arguments which are two numbers that the user inputs. //The function should return the product of the numbers. double Product(double firstNumber,double secondNumber){ return firstNumber*secondNumber; } //function Quotient() that has two arguments, which are the numbers input by the user. //The function should return the quotient of the first number divided by the second double Quotient(double firstNumber,double secondNumber){ return firstNumber/secondNumber; } //main method int main() { //promt user to enter number cout<<"Enter first number: "; double firstNumber; //read number cin>>firstNumber; cout<<"Enter second number: "; double secondNumber; //read number cin>>secondNumber; cout<<"Product = "<<Product(firstNumber,secondNumber)<<"\n"; const double epsilon =1e-10; // If the second number is zero (recall that division by zero is not allowed). display an error message and exit the program. if(fabs(secondNumber)/epsilon==0){ //show result cout<<"Division by zero is not allowed!\n"; }else{ //show result of Quotient cout<<"Quotient = "<<Quotient(firstNumber,secondNumber)<<"\n"; } //delay system("pause"); //return 0 return 0; }
Comments
Leave a comment