19 yields 3 fives and 4 ones. Write a single statement that assigns the number of one dollar bills to variable numOnes , cashier distributes change using the maximum number of five dollar bills , followed by one dollar bills. For example
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
2.7.2 Compute Change
A cashier distributes change using the maximum number of five dollar bills, followed by one dollar bills. For example, 19 yields 3 fives and 4 ones. Write a single statement that assigns the number of one dollar bills to variable numOnes, given amountToChange. Hint: Use the % operator.
import java.util.Scanner;
public class ComputingChange {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int amountToChange;
int numFives;
int numOnes;
amountToChange = scnr.nextInt();
numFives = amountToChange / 5;
/* Your solution goes here */
System.out.print(“numFives: “);
System.out.println(numFives);
System.out.print(“numOnes: “);
System.out.println(numOnes);
}
}
Answer ( 1 )
Please briefly explain why you feel this answer should be reported .
The single statement that assigns the number of one dollar bills to the variable numOnes is:
numOnes = amountToChange % 5;
This statement uses the % operator to compute the remainder when amountToChange is divided by 5, which gives the number of dollars that cannot be represented using a five dollar bill. These dollars are given in one dollar bills, so we assign this value to the variable numOnes.