- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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 the number of currency notes needed in C++

Given an amount in rupees that one has to pay, say pay_rupees and an unlimited amount of banknotes that have value as Rupees_amount_1 and Rupees_amount_2. The goal is to pay pay_rupees using exactly the total number of notes=distribution_total and count the number of type Rupees_amount_1 notes required. If there is no solution to pay then return −1 as answer.

**For Example**

## Input

Rupees_amount_1 = 1, Rupees_amount_2 = 5, pay_Rupees = 11 distribution_total = 7

## Output

Count of number of currency notes needed are − 6

## Explanation

6*1 + 5*1 = 11 and notes=6+1=7

## Input

Rupees_amount_1 = 2, Rupees_amount_2 = 3, pay_Rupees = 10 distribution_total = 4

## Output

Count of number of currency notes needed are: 2

## Explanation

2*2 + 3*2 = 10 and notes=2+2=4

**Approach used in the below program is as follows** −

Let a1 be the number of notes of amount rupees 1and N be the total number of notes. To make the payment of P, we will have equations − a1*(Rupees_amount_1) + (N−a1)*(Rupees_amount_2) = P P=a1*(Rupees_amount_1) + (N)*(Rupees_amount_2) − (a1)*(Rupees_amount_2) P − (N)*(Rupees_amount_2) =a1*(Rupees_amount_1) − (a1)*(Rupees_amount_2) a1= P − (N)*(Rupees_amount_2) / ( Rupees_amount_1 − Rupees_amount_2) If a1 is integer then only we will have a solution else return −1.

Take all numbers as input.

Function notes_needed(int Rupees_amount_1, int Rupees_amount_2, int pay_Rupees, int distribution_total) takes all inputs and returns the count of the number of currency notes needed.

Take the initial count as 0.

Take total = pay_Rupees − (Rupees_amount_2 * distribution_total)

Set total_given = Rupees_amount_1 − Rupees_amount_2

Set total_given = Rupees_amount_1 − Rupees_amount_2

If total % total_given == 0 then return count as total / total_given.

Else return −1.

## Example

#include<bits/stdc++.h> using namespace std; int notes_needed(int Rupees_amount_1, int Rupees_amount_2, int pay_Rupees, int distribution_total){ int count = 0; int total = pay_Rupees − (Rupees_amount_2 * distribution_total); int total_given = Rupees_amount_1 − Rupees_amount_2; if (total % total_given == 0){ count = total / total_given; return count; } else { return −1; } } int main(){ int Rupees_amount_1 = 1; int Rupees_amount_2 = 5; int pay_Rupees = 11; int distribution_total = 7; cout<<"Count of number of currency notes needed are: "<<notes_needed(Rupees_amount_1, Rupees_amount_2, pay_Rupees, distribution_total); }

## Output

If we run the above code it will generate the following output −

Count the number of currency notes needed are− 6

- Related Questions & Answers
- Find minimum number of currency notes and values that sum to given amount in C++
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to count expected number of operations needed for all node removal
- C++ program to count number of minimum coins needed to get sum k
- Finding least number of notes to sum an amount - JavaScript
- C++ program to count number of operations needed to reach n by paying coins
- C++ program to count minimum number of binary digit numbers needed to represent n
- C++ program to count number of steps needed to make sum and the product different from zero
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- C++ program to count number of minutes needed to increase stick length to form a triangle
- What is the difference between floating currency and fixed currency?
- C++ Program to count number of operations needed to place elements whose index is smaller than value
- Program to find number of coins needed to make the changes in Python
- Effective Ways of Preparing Notes for Students
- What is the definition of a key currency?