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<<"\nHello "<<initials<<"., let's see what your coins are worth."<<::std::endl;
//coins request and display
std::cout<<"\nEnter number of quarters: "; std::cin>>quarters;
std::cout<<"\nEnter number of dimes: "; std::cin>>dimes;
std::cout<<"\nEnter number of nickels: "; std::cin>>nickels;
std::cout<<"\nEnter number of pennies: "; std::cin>>pennies;
std::cout<<"\n\nNumber of quarters is "<<quarters;
std::cout<<"\nNumber of dimes is "<<dimes;
std::cout<<"\nNumber of nickels is "<<nickels;
std::cout<<"\nNumber 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<<"\n\nYour coins are worth "<<dollars<<" dollars and "<<cents<<" cents."<<::std::endl;
return 0;
}
Explanation:
Code is written in C++ language.
- First we declare the <em>variables </em>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 <em>welcome message</em>.
- 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.