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.