You have a large collection of pennies and wish to change them into dollar bills at the bank, with quarters, dimes, nickels, and pennies being used for any remainder. Prompt for the number of pennies and produce output as shown below.
Output:
How many pennies do you have (must be greater than 100)? [Assume user inputs 641]
641 pennies can be changed at the bank as follows:
Dollars: 6
Quarters: 1
Dimes: 1
Nickels: 1
Pennies: 1
Solve in C++.
In the given problem we have to write a code which gives the output in number of dollars, quarters, dimes , nickles and pennies with the given input of no. of pennies.
In order to solve the problem let us have the conversion rate
1 Dollar = 100 pennies
1 Quarter = 25 pennies
1 dime = 10 pennies
1 nickle = 5 pennies
Let me explain the Approach I am going to use
1. In order to solve the problem we have to divide the given pennies into parts:-
Step 1: Now first we need to find the no. of dollars considering 1 Dollar = 100 pennies
No. of dollars = Total pennies/100;
Reminder of dollars = Total Pennies %100; ( In order to find the remainder we use % operator)
Step 2: Now to find the no. of quarters we will divide the Reminder of dollars by 25 as 1 Quarter = 25 pennies
No. of Quarter = Reminder of dollars / 25;
Reminder of Quarter = Reminder of dollars % 25;
Step 3: Now to find the no. of Dimes we will divide the Reminder of Quarter by 10 as 1 Dime = 10 pennies
No. of Dime= Reminder of Quarter / 10;
Reminder of Dime = Reminder of Quarter % 10;
Step 4: Now to find the no. of Nickles we will divide the Reminder of Dime by 5 as 1 Nickle = 5 pennies
No. of Nickles = Reminder of Dime / 5;
Remaining pennies = Reminder of Dime% 5;
Hence we will get the answer.
Now let us write the code:-
#include<iostream> // Header file
using namespace std;
int main()
{
int p , d ,qu ,di , ni , pen; // declaring the
variables
int d1,q1,n1,di1; // declaring the reminders(you can use any
name)
cout<<" How many pennies do you have (must be greater than
100)?";
cin>>p; // Taking the input
d = p / 100; // finding the dollars
d1 = p % 100; // finding reminder for quarter
qu = d1 / 25; // finding the quarters
q1 = d1 % 25; // finding reminder for dimes
di = q1 / 10; // finding the dimes
d1 = q1 % 10; // finding reminder for nickles
ni = di1 / 5; // finding the nickels
pen = di1 % 5; // finding reminder for the remaining pennies
//output
cout<<"\n Dollars:"<<d;
cout<<"\n Quarters: "<<qu;
cout<<" \n Dimes:"<<di;
cout<<" \n Nickles"<<ni;
cout<<"\n Pennies"<<pen;
}
Here is the code , you may find the error stray 240 in program if you copy paste the code , this is common is IDEs hence I'll request you to run the unwanted spaces and then run the program.
Feel free to reach out to me in comment section.
Get Answers For Free
Most questions answered within 1 hours.