
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Coins of Each Type from Given Ratio in C++
In this problem, we are given four numbers that define the totalPrice and the ratio of coins of 1 Rs, 50 paise, 25 paise in the bag. Our task is to create a program to find the count of coins of each type from the given ratio in C++.
Code description − Here, we need to use the coins of 1 Rs, 50 paise, and 25 paise from the bag to give the total sum of coins to the given total.
Let’s take an example to understand the problem,
Input
TotalPrice = 225, 1Rs = 2, 50P = 3, 25P = 4
Output
1 Rs coin = 100 50 P coin = 150 25 P coin = 200
Explanation
Considering the ratio, the value of coins that make up the value.
1 RE coin 2X, 50 paise coin 1.5X, 25 paise coin 1X,
Summing up all values = 225
2X + 1.5X + 1X = 225 4.5X = 225 => X = 225/4.5 = 50,
Value of 1Re coins = 100, coins 100
Value of 50 paise coins = 75, coins 150
Value of 25 paise coins = 50, coins 200
Solution Approach
As in the explanation, we will find the amount contributed by each coin to the sum. And then find the number of coins each value based on the value. For 1 Re coins, X is the number of coins.
For 50 paise coins, 2X is the number of coins.
For 25 paise coins, 4X is the number of coins.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int CalcCoinValue(int totalPrice, int re1, int p50, int p25) { float val1Coin = (re1 * 1.0), val50pCoin = (p50/2.0), val25pCoin =(p25/4.0); int result = totalPrice / (val1Coin + val50pCoin + val25pCoin); return result; } int main() { int totalPrice = 225; int re1 = 2, p50 = 3, p25 = 4; int coinValue = CalcCoinValue(totalPrice, re1, p50, p25); cout<<"Count of 1 rupee coin = " <<(coinValue * re1)<<endl; cout<<"Count of 50 paise coin = " <<(coinValue * p50)<<endl; cout<<"Count of 25 paise coin = " <<(coinValue * p25)<<endl; return 0; }
Output
Count of 1 rupee coin = 100 Count of 50 paise coin = 150 Count of 25 paise coin = 200