11. Create a three-function calculator for old-style English currency. The calculator should allow the user to add or subtract two money amounts, or to multiply a money amount by a floating-point number.
Chapter 3:
Loops and Decisions
Programming Exercise
Problem # 11:
Create a three-function calculator for old-style English currency, where money amounts are specified in pounds, shillings, and pence. (See Exercises 10 and 12 in Chapter 2.) The calculator should allow the user to add or subtract two money amounts, or to multiply a money amount by a floating-point number. (It doesn’t
make sense to multiply two money amounts; there is no such thing as square money. We’ll ignore division. Use the general style of the ordinary four-function
calculator in Exercise 4 in this chapter.)
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int pound1, shilling1, pence1, pound2, shilling2, pence2, poundRes, shillingRes, penceRes;
float num;
char ch;
cout<<"Input operator: ";
ch=getche();
switch(ch)
{
case '+':
cout<<endl<<"Enter first amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound1;
cout<<"Enter Shilling: ";
cin>>shilling1;
cout<<"Enter Pence: ";
cin>>pence1;
cout<<"Enter second amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound2;
cout<<"Enter Shilling: ";
cin>>shilling2;
cout<<"Enter Pence: ";
cin>>pence2;
poundRes=pound1+pound2;
shillingRes=shilling1+shilling2;
penceRes=pence1+pence2;
break;
case '-':
cout<<endl<<"Enter first amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound1;
cout<<"Enter Shilling: ";
cin>>shilling1;
cout<<"Enter Pence: ";
cin>>pence1;
cout<<"Enter second amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound2;
cout<<"Enter Shilling: ";
cin>>shilling2;
cout<<"Enter Pence: ";
cin>>pence2;
poundRes=pound1-pound2;
shillingRes=shilling1-shilling2;
penceRes=pence1-pence2;
break;
case '*':
cout<<endl<<"Enter amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound1;
cout<<"Enter Shilling: ";
cin>>shilling1;
cout<<"Enter Pence: ";
cin>>pence1;
cout<<"Enter a number to multiply amount: ";
cin>>num;
poundRes=pound1*num;
shillingRes=shilling1*num;
penceRes=pence1*num;
break;
default:
cout<<"Invalid input.";
}
if (penceRes >= 12)
{
shillingRes = shillingRes + (penceRes/12);
penceRes = penceRes%12;
}
if (shillingRes >= 20)
{
poundRes = poundRes + (shillingRes/20);
shillingRes = shillingRes%20;
}
cout<<"Total is :\x9c"<<poundRes<<"."<<shillingRes<<"."<<penceRes<<endl;
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
int pound1, shilling1, pence1, pound2, shilling2, pence2, poundRes, shillingRes, penceRes;
float num;
char ch;
cout<<"Input operator: ";
ch=getche();
switch(ch)
{
case '+':
cout<<endl<<"Enter first amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound1;
cout<<"Enter Shilling: ";
cin>>shilling1;
cout<<"Enter Pence: ";
cin>>pence1;
cout<<"Enter second amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound2;
cout<<"Enter Shilling: ";
cin>>shilling2;
cout<<"Enter Pence: ";
cin>>pence2;
poundRes=pound1+pound2;
shillingRes=shilling1+shilling2;
penceRes=pence1+pence2;
break;
case '-':
cout<<endl<<"Enter first amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound1;
cout<<"Enter Shilling: ";
cin>>shilling1;
cout<<"Enter Pence: ";
cin>>pence1;
cout<<"Enter second amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound2;
cout<<"Enter Shilling: ";
cin>>shilling2;
cout<<"Enter Pence: ";
cin>>pence2;
poundRes=pound1-pound2;
shillingRes=shilling1-shilling2;
penceRes=pence1-pence2;
break;
case '*':
cout<<endl<<"Enter amount: \x9c"<<endl;
cout<<"Enter Pound: ";
cin>>pound1;
cout<<"Enter Shilling: ";
cin>>shilling1;
cout<<"Enter Pence: ";
cin>>pence1;
cout<<"Enter a number to multiply amount: ";
cin>>num;
poundRes=pound1*num;
shillingRes=shilling1*num;
penceRes=pence1*num;
break;
default:
cout<<"Invalid input.";
}
if (penceRes >= 12)
{
shillingRes = shillingRes + (penceRes/12);
penceRes = penceRes%12;
}
if (shillingRes >= 20)
{
poundRes = poundRes + (shillingRes/20);
shillingRes = shillingRes%20;
}
cout<<"Total is :\x9c"<<poundRes<<"."<<shillingRes<<"."<<penceRes<<endl;
return 0;
}
Let me know in the comment section if you have any question.
Similar Posts:
- Program that allows the user to enter the number and then generates the table, formatting it into 10 columns and 20 lines.
- Temperature-conversion program that gives the user the option of converting Fahrenheit to Celsius or Celsius to Fahrenheit. Then carry out the conversion.
- Program that should allow the user to type up to six digits, and then display the resulting number as a type long integer.
- Program should ask the user to enter a number, an operator, and another number. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Finally, display the result.
- Use for loops to construct a program that displays a pyramid of Xs on the screen.
- Modify the FACTOR program in this chapter so that it repeatedly asks for a number and calculates its factorial, until the user enters 0, at which point it terminates.
- Program that calculates how much money you’ll end up with if you invest an amount of money at a fixed interest rate, compounded yearly. Have the user furnish the initial amount, the number of years, and the yearly interest rate in percent.
- Program that repeatedly asks the user to enter two money amounts expressed in old-style British currency: pounds, shillings, and pence. The program should then add the two amounts and display the answer, again in pounds, shillings, and pence. Use a do loop that asks the user whether the program should be terminated.
- Program where you tell the final amount and it figures out how many years it will take, at a fixed rate of interest compounded yearly, to reach this amount.
- Create a three-function calculator for old-style English currency. The calculator should allow the user to add or subtract two money amounts, or to multiply a money amount by a floating-point number.
- Create a four-function calculator for fractions. The user should type the first fraction, an operator, and a second fraction. The program should then display the result and ask whether the user wants to continue.
Comments
Post a Comment