Solving the Exact Change Problem in Python If Conditions

Handling Exact Change With If Statements In Python

The ‘if’ statement is one of the most widely used conditional statements in Python. It is simple and easy to implement as is one of the very first things that beginners learn when they are learning any programming language. Even at a professional level, it forms an essential part of almost every program and software. Regardless of the language that you use, the ‘if’ statement is used by every developer all over the world.

One common problem that can be solved using if statements is determining the minimum or the exact number of rupee notes or coins or dollar bills or any other currency required to make a specific amount of money for transaction or speculation purposes. This is important in building applications or user interfaces for vending machines and in other financial calculators used for calculating interest on loans.

In this article, we will see how we can design a simple program to handle exact change with if statements in Python step-by-step. Let’s get started!

Understanding the Exact Change Problem

Let’s understand this problem through a simple example. Let’s say we need 500.35 rupees in various denominations such as 10 rupees notes, 5 rupees coins, and some other denominations of your choice. We also need to determine the minimum number of notes or coins required to make this amount of money.

It might seem simple at first, but on a larger scale such as interests paid by banks on fixed deposits or any other type of funds, every single digit after the decimal point matters. This is because for a small amount such as 100 rupees, an interest of 3.3% will only be rupees 3.3, but for a huge amount of 1 crore, 3.3% amounts to rupees 33 lakhs. This is a huge amount, hence we need to determine the exact change when principal amounts are very large carefully.

Suggested: Debugging Elif Syntax Errors in Python.

Calculating Exact Change with Conditionals in Python

In this section, we will be using the if-else conditional statements in Python to solve the exact change problem. Let’s get started. We will be using INR or the Indian Rupees as our default currency.

  • Step 1: First, we will initialize the denominations of the rupee notes in an array. The denominations are: 2000, 500, 200,100,50,20,10, 2 and 1.
denominations = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]  # Denominations of Indian Rupees
  • Step 2: Next, we will define our variables and take user input for the amount for which we have to calculate the minimum amount of notes required for this amount, which will be given by count.
amount = float(input("Enter your desired amount in INR= "))  # Amount in Rupees
count = [0] * len(denominations)  # Initializing count for each denomination
  • Step 3: We will use a for loop to iterate through each denomination and determine how many of each denomination is required for the specified amount.
for i, coin in enumerate(denominations):
    count[i] = amount // coin  # Calculating count of current denomination
    amount %= coin  # Updating remaining amount
  • Step 4: In this step we will be using the if statement to determine which denomination has a value greater than 0 so that we can display the denomination along with the number of notes required of the denomination for the specified amount.
# Displaying results
for i, coin in enumerate(denominations):
    if count[i] > 0:
        print(f"₹{coin}: {count[i]}")

When we run the problem, it will prompt us for an amount, for example, I have given an input of 568 and it will return the required denominations with the minimum number of each type of denomination required to achieve the given amount. The output is:

Enter your desired amount in INR= 568
₹500: 1.0
₹50: 1.0
₹10: 1.0
₹5: 1.0
₹2: 1.0
₹1: 1.0

The entire code is given below:

denominations = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]  # Denominations of Indian Rupees
amount = float(input("Enter your desired amount in INR= "))  # Amount in Rupees
count = [0] * len(denominations)  # Initializing count for each denomination
for i, coin in enumerate(denominations):
    count[i] = amount // coin  # Calculating count of current denomination
    amount %= coin  # Updating remaining amount

# Displaying results
for i, coin in enumerate(denominations):
    if count[i] > 0:
        print(f"₹{coin}: {count[i]}")
Handling Exact Change With If Statements In Python 1
Calculating Minimum Denominations for a Given Amount

Recommended: Round Up Numbers in Python Without math.ceil()

Inference.

In this article, we have gone through what the exact change problem is and how it affects us in our daily lives. There are also many advantages of having an application that will tell us the quantity of money we need to carry in specific denominations for a particular expense. This comes in very handy for travelers who are traveling to new places often and are not versed with the local currency very well. Luckily for us, this problem can be very easily solved in Python using the if-else conditional statement. We hope you found this article helpful!