Calculates the breakdown of a dollar amount into the minimum
number of currency denominations. It avoids floating-point errors by converting
the dollar amount into cents and operates in integer arithmetic.
Java
import
java.util.Scanner; public class
CurrencyBreakdown { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the
dollar amount (e.g., 123.45): "); double amount = scanner.nextDouble(); // Convert amount to cents to avoid
floating-point errors int cents = (int) Math.round(amount * 100); // Denominations in cents int[] denominations = {10000, 5000,
2000, 1000, 500, 100, 25, 10, 5, 1}; String[] names = {"$100
bill", "$50 bill", "$20 bill", "$10 bill",
"$5 bill", "$1
bill", "Quarter (25¢)", "Dime (10¢)", "Nickel
(5¢)", "Penny (1¢)"}; System.out.println("Minimum
number of denominations needed:"); for (int i = 0; i <
denominations.length; i++) { int count = cents /
denominations[i]; // Calculate how
many of each denomination if (count > 0) { System.out.println(count +
" x " + names[i]); } cents %= denominations[i]; // Get the remaining amount } scanner.close(); } } |
Step-by-Step Walkthrough
- Input
Dollar Amount:
Ø
The program prompts the user to enter a dollar
amount (e.g., 123.45).
Ø
It uses a Scanner to read the input as a double.
java
System.out.print("Enter
the dollar amount (e.g., 123.45): "); double amount
= scanner.nextDouble(); |
- Convert
to Cents:
Ø
To avoid floating-point arithmetic issues, the
dollar amount is converted into cents by multiplying it by 100 and rounding to
the nearest integer.
java
int cents =
(int) Math.round(amount * 100); |
Ø
For example: 123.45×100=12345cents
- Denominations:
Ø
Denominations are defined as an array in cents
for easy calculations. Their corresponding names are stored in a String array
for display.
java
int[]
denominations = {10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1}; String[]
names = {"$100 bill", "$50 bill", "$20 bill",
"$10 bill", "$5 bill", "$1 bill",
"Quarter (25¢)", "Dime (10¢)", "Nickel (5¢)",
"Penny (1¢)"}; |
- Calculation
Logic:
Ø
Loop Through Denominations: The program
iterates through each denomination.
Ø
Count the Number of Denominations:
ü
It calculates how many of each denomination can
fit into the remaining amount of cents using integer division.
ü
Updates the remaining cents using the modulus
operator.
java
for (int i =
0; i < denominations.length; i++) { int count = cents /
denominations[i]; // Calculate count
of current denomination if (count > 0) { System.out.println(count + " x
" + names[i]); } cents %= denominations[i]; // Update remaining cents } |
Example:
Ø
Input: 123.45
Ø
Initial cents = 12345
Ø
Iterations:
ü
$100 bill: \(⌊\frac{12345}{10000}⌋=1\),
Remaining 12345%10000=2345
ü
$20 bill: \(⌊\frac{2345}{2000}⌋=1\),
Remaining 2345%2000=345
ü
$10 bill: \(⌊\frac{345}{1000}⌋=0\), Remaining
345
ü
$5 bill: \(⌊\frac{345}{500}⌋=0\), Remaining 345
ü
$1 bill: \(⌊\frac{345}{100}⌋=3\), Remaining 345%100=45
ü
Quarter: \(⌊\frac{45}{25}⌋=1\), Remaining 45%25=20
ü
Dime: \(⌊\frac{20}{10}⌋=2\), Remaining 20%10=0
Final Output:
bash
1 x $100 bill 1 x $20 bill 3 x $1 bill 1 x Quarter
(25¢) 2 x Dime
(10¢) |
- Output:
Ø
The program prints the number of each
denomination needed to make up the dollar amount.
- Close
Scanner:
Ø
The scanner is closed to release resources.
java
scanner.close(); |
Mathematical Formulation
The program uses greedy algorithm principles to
determine the optimal breakdown of denominations.
- Denomination Calculation: For each denomination \(d_{i}\):
- $$count_{i}=⌊\frac{cents}{d_{i}}⌋$$
Remaining cents after using \(d_{i}\):
$$cents=cents\text{%}d_{i}$$
- Greedy
Algorithm Justification:
- This
approach ensures that the largest denomination is used first, minimizing
the number of bills and coins.
Advantages of This Approach
Ø Avoids
Floating-Point Errors: Working with integer cents avoids inaccuracies
inherent in floating-point arithmetic.
Ø Efficient
and Optimal: Greedy algorithm minimizes the number of denominations used.
Potential Enhancements
- Error
Handling: Validate the input to ensure it's a valid non-negative
number.
java
if (amount
< 0) { System.out.println("Invalid amount.
Please enter a positive number."); return; } |
- Support
for Other Currencies: Adapt denominations for different currencies by
changing the denominations and names arrays.
- Dynamic
Denominations: Allow the user to input custom denominations and their
names.
This program demonstrates the application of mathematics in
solving real-world problems such as currency breakdown efficiently!
Dollar amount into the minimum number of currency denominations |
For More DSA Related information, visit
Ø
Bench
mark of compiler using Ackerman function
Ø
To
check if the rows of a matrix are circularly identical in Java
Ø
Frequency
Weaving Logic & Spiral Printing of a Rectangle
Ø
Zig Zag
Matrix print multiple way
Ø
Greedy
Algorithm’s or knapsack algorithms
Ø
understanding
recursive method for binary tree
Ø
Dynamic
Programming: Max Square Sub-matrix of 1s in a Matrix
Ø
Previous and
Next Date Palindrome
Ø
Karatsuba's
Algorithm for multiplying two large numbers
Ø
Multiplication
In different Way
Ø
How
to draw a Tree from array of integer
Ø
Position
of robot after given movements
Ø
Alphanumeric
Random Number generator
Ø
Solving
Tiger-Goat-Boatman Puzzle: BFS & DFS River Crossing
Ø
Dijsktra
Shortest Path for directed an undirected graph
For More Java Related information, visit
Ø
Streams
Lambdas Collectors and Functional Interfaces in Java 8
Ø
Java
8 support static method or default method or both in the interface
Ø
Serialization
understanding
Ø
Garbage
Collection Under Standing
Ø
How
work Garbage Collection in Java
Ø
Under
Standing Of Mutable and Immutable Class
For More sort information, visit:
Ø
Selection
Sort with iteration wise per step
Ø
Bubble
Sort with each iteration how it is work
Ø
Merge
sort of Each step how it is working
Ø
Quick
Sort per iteration what happen
Ø
Sorting
Of a list multiple attribute wise two technique
Ø
Seat
Arrangement in Sorting Order Like 1A-1E, 3C-3G etc
Ø
How
to sort 10 billion numbers
Ø
Merge
Sort simple under standing
For Math information, visit:
Ø Calculating the area of a triangle
For Design information, visit:
Ø
Mastering
Design Patterns Practical Implementation Tips
Ø
How
to draw sequence diagram and other diagrams using plantuml
Ø
Time
Analysis for Congested Routes Using Shortest Path Algorithms
Ø Passive
Infrared (PIR) sensors understand and implementation by java
0 Comments