JAVA Design a program to give customers the correct change. 1) Coins to use and
ID: 3848073 • Letter: J
Question
JAVA
Design a program to give customers the correct change.
1) Coins to use and their values:
Half Dollars (50)
Quarter Dollars (25)
Dimes (10)
Nickels (5)
Pennies (1)
The user will enter a number between 1 and 99. If the number is outside of that range, you will inform the user and terminate the program.
You will use as few coins as possible. For example, if the user enters "50", you will give the answer as "One Half Dollar" and not "5 Dimes" or "50 Pennies".
You will loop through each coin denomination, with the possible exception of the penny.
Here is a hint: If the number is 99, then subtract 50 from the number using a loop.
This leaves 49, with one half dollar used. Next, subtract 25 from the total using a loop. This leaves 24, with one quarter used Next, subtract 10 cents from the total in a loop. This will leave 4 cents, with two dimes used. Finally record the four pennies left over. The output should read something like: "You have one half dollar, one quarter, two dimes, and four pennies".
Explanation / Answer
package sample1;
import java.util.Scanner;
import java.io.*;
public class test {
public static void main(String args[]) throws IOException
{
Scanner s=new Scanner(System.in);
System.out.println(" enter how many coins (1 to 99) : ");
int n=s.nextInt();
if(n>=1&&n<=99){
int half=0;
int quarter=0;
int dime=0;
int nickel=0;
int pennies=0;
half=n/50;
n=n%50;
quarter=n/25;
n=n%25;
dime=n/10;
n=n%10;
nickel=n/5;
n=n%5;
pennies=n;
System.out.println("You have "+half+" half dollars,"+quarter+" quarter dollars,"+dime+" dimes,"+nickel+" nickels and "+pennies+" pennies");
}
else{
System.out.println(" invalid input ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.