TOPICS Parameter Passing Pass By Value Copy Pass By Reference Alias DESCRIPTION
ID: 3567724 • Letter: T
Question
TOPICS Parameter Passing Pass By Value Copy Pass By Reference Alias DESCRIPTION Write a program that will prompt the user to input two decimal numbers and will display their sum. difference, product and quotient. The sum is the result of addition. difference that of subtraction, product that of multiplication and quotient that of division.) The program will do this repeatedly. Java users The program will end if the user presses Cancel button when prompted for the first number. C- users The program will end if the user enters N when prompted for the first number Instructions For each of the methods (main, compute), provide functional description, parameter description, and return value description just above the method code. Testing Enter First Number 2.4 Enter Second Number First Number: 2.4 Second Number: 1.2 Sum: 3.6 Difference: 1.2 Product: 2. Quotient: 2.0 Enter First Number Enter Second Number First Number: 4.5 Second Number: 1.5 Sum: 6.0 Difference: 3.0 Product: 6.75 Quotient: 3.0 Enter First Number Press Cancel Button (Java users) (C+ usersExplanation / Answer
#include <iostream>
#include<stdlib.h>
using namespace std;
//takes two params to calculate sum
float sum(float a, float b)
{
return a+b;
}
//takes two params to calculate difference
float difference(float a, float b)
{
return a-b;
}
//takes two params to calculate product
float product(float a, float b)
{
return a*b;
}
//takes two params to calculate quotient
float quotient(float a, float b)
{
return a/b;
}
int main()
{
char *s = new char[10];
float a,b;
while(true)
{
cout<<"Enter First Number: ";
cin>>s;
if(s[0]=='x')
break;
a=atof(s);
cout<<" Enter Second Number: ";
cin>>b;
cout<<" First Number: "<<a;
cout<<" Second Number: "<<b;
cout<<" Sum:"<<sum(a, b);
cout<<" Difference:"<<difference(a, b);
cout<<" Product:"<<product(a, b);
cout<<" Quotient:"<<quotient(a, b);
cout<<" ";
}
return 0;
}
----------------------
OUTPUT
x
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.