139k views
6 votes
Write a program that gets three input characters which are user's initials and displays them in a welcoming message. Then gets input of the quantities of each of the following coins, in the respective order, quarters, dimes, nickels, and pennies.

Computes the total value of the coins, and then displays the value.
Enter the initials with no space, e.g. JTK.
Here are sample runs (blue user input):
Test Run 1
Enter your initials, first, middle and last: JTK Hello J.T.K., let's see what your coins are worth.
Enter number of quarters: 4
Enter number of dimes: 0
Enter number of nickels: 0
Enter number of pennies: 0
Number of quarters is 4.
Number of dimes is 0.
Number of nickels is 0.
Number of pennies is 0.
Your coins are worth 1 dollars and 0 cents.
Test Run 2 Enter your initials, first, middle and last: RHT
Hello R.H.T., let's see what your coins are worth.
Enter number of quarters: 0
Enter number of dimes: 10
Enter number of nickels: 0
Enter number of pennies: 0
Number of quarters is 0.
Number of dimes is 10.
Number of nickels is 0.
Number of pennies is 0.
Your coins are worth 1 dollars and 0 cents.

1 Answer

2 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

// variable declaration

string initials;

int quarters;

int dimes;

int nickels;

int pennies;

float total;

int dollars;

int cents;

//initials request

std::cout<<"Enter your initials, first, middle and last: "; std::cin>>initials;

//welcome message

std::cout<<"\\Hello "<<initials<<"., let's see what your coins are worth."<<::std::endl;

//coins request and display

std::cout<<"\\Enter number of quarters: "; std::cin>>quarters;

std::cout<<"\\Enter number of dimes: "; std::cin>>dimes;

std::cout<<"\\Enter number of nickels: "; std::cin>>nickels;

std::cout<<"\\Enter number of pennies: "; std::cin>>pennies;

std::cout<<"\\\\Number of quarters is "<<quarters;

std::cout<<"\\Number of dimes is "<<dimes;

std::cout<<"\\Number of nickels is "<<nickels;

std::cout<<"\\Number of pennies is "<<pennies;

//total value calculation

total = quarters*0.25+dimes*0.1+nickels*0.05+pennies*0.01;

dollars = (int) total;

cents = (total-dollars)*100;

std::cout<<"\\\\Your coins are worth "<<dollars<<" dollars and "<<cents<<" cents."<<::std::endl;

return 0;

}

Step-by-step explanation:

Code is written in C++ language.

  • First we declare the variables to be used for user input and calculations.
  • Then we show a text on screen requesting the user to input his/her initials, and displaying a welcome message.
  • The third section successively request the user to input the number of quarters, dimes, nickels and pennies to count.
  • Finally the program calculates the total value, by giving each type of coin its corresponding value (0.25 for quarters, 0.1 for dimes, 0.05 for nickels and 0.01 for pennies) and multiplying for the number of each coin.
  • To split the total value into dollars and cents, the program takes the total variable (of type float) and stores it into the dollars variable (of type int) since int does not store decimals, it only stores the integer part.
  • Once the dollar value is obtained, it is subtracted from the total value, leaving only the cents part.

User Kevin Tsoi
by
3.7k points