In: Computer Science
Create a program/function using PYTHON that takes cents and returns to the customer how MANY coins it takes to make the change...
Ex. if the change owed is 50 cents then return a 2 (for two quarters)
if the change owed is 10 cents then return a 1 (for one dime)
AGAIN please write this in java and please provide EXPLANATION of answer
1 dime = 10 cents
1 quarter = 25 cents
So we need to break up the cents given by user as input either as dime or quarter or combination of both.
Python Program
Code
cents = int(input("Please enter the change in Cents: "))
# example 65 cents
# quarter = 65/25 = 2
# dime = (65%25)/10 = 15/10 = 1
# cent = (65%25)%10 = (15)%10 = 5
# output = 2 quarter and 1 dime
quarter = int(cents/25) # 25 cents = 1 quarter
dime = int((cents%25)/10) # 10 cents = 1 dime
cents = (cents%25)%10
print(quarter,"quarters",dime,"dimes and",cents,"cents")
Test Output
Java Program
Code
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println("Please enter the change in cents:
");
String input = System.console().readLine();
int cents = Integer.parseInt(input);
// example 65 cents
// quarter = 65/25 = 2
// dime = (65%25)/10 = 15/10 = 1
// cent = (65%25)%10 = (15)%10 = 5
// output = 2 quarter and 1 dime
int quarter = cents/25; // 25 cents = 1 quarter
int dime = (cents%25)/10; // 10 cents = 1 dime
cents = (cents%25)%10;
System.out.println(quarter + "
quarters " + dime + " dimes and "+ cents + " cents");
}
}
Test Output