Challenge
Write a program to convert cents as input to dollars, quarters, dimes, nickels, and pennies.
Assumptions
- The input number is > 0.
Input
- The input number as cents.
Output
- The number converted to smaller currency denominations.
Requirements
- If any denomination (dollars, quarters, dimes, nickels, and pennies) has 0 units, don't display that denomination in the output. e.g. the input number 12, display the output as 1 dime, 2 pennies instead of 0 dollars, 0 quarters, 1 dimes, 0 nickels, 2 pennies
- Display the denominations in output as singular or plural if the value of that denomination is one or more than one respectively. e.g. if the input number is 146, display the output as 1 dollar, 1 quarter, 2 dimes, 1 penny instead of 1 dollars, 1 quarters, 2 dimes, 1 pennies.
Test cases
Input | Number (amount) | Output |
---|---|---|
1 | 248 | 2 dollars, 1 quarter, 2 dimes, 3 pennies |
2 | 245 | 2 dollars, 1 quarter, 2 dimes |
3 | 257 | 2 dollars, 2 quarters, 1 nickel, 2 pennies |
4 | 51 | 2 quarters, 1 penny |
5 | 6 | 1 nickel, 1 penny |
6 | 5 | 1 nickel |
7 | 100 | 1 dollar |
Instructions
- Accept the input number of cents via the command line.
- Write the logic to compute various denominations(dollars, quarters, dimes, nickels, and pennies).
- Pay attention to the output, the way comma and spaces are separate the number and denomination. Output has to exactly match.
- Display the resulting output.