Answer to Question #237039 in Java | JSP | JSF for Selma Shigwedha

Question #237039
Ever wondered how change is disbursed to customers in certain stores? In Namibia the following
notes exists 200,100,50,20,10 and following coins: 5, 1, 50c, 10c, 5c. For the sake of this exercises
let us only consider change below 50 see below example:
Sample run 1:
Enter item name: Apples
Enter QTY of Apples: 2
Enter price per item N$: 3.25
Amount tendered N$: 50
Your change is: N$ 43.50
Disbursed as follows: 2 x N$20; 0 x N$10; 0 x N$5; 3 x N$1; 1 x 50c; 0 x 10c; 0 x 5c
[Hint: make use of % and / operators to determine the values needed, see Pg. 113 for more info.]
1
Expert's answer
2021-09-14T18:21:02-0400
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("\nEnter Item name: ");
        
        String item_name = in.next();
        System.out.printf("Enter QTY of %s\n", item_name);
        int QTY = in.nextInt();
        System.out.println("Enter price per item of N$: \n");
        float item_price = in.nextFloat();
        
        System.out.println("Amount tendered N$: \n");
        float amount_tendered = in.nextFloat();
        float change = amount_tendered - (QTY * item_price);
        System.out.println(" Your change is N$: "+change);
        
        int  n_20, n_10, n_5, n_1, n_50c, n_10c, n_5c;
        float rem =  change % 20;
         n_20 = (int)change / 20;
         change = rem;
         rem = rem % 10;
        n_10 = (int) change / 10;
        
        change = rem;
        rem = rem % 5;
        n_5= (int) change / 5;
        
        change = rem;
        rem = rem % 1;
        n_1= (int) change / 1;
        
        change = rem;
        rem = rem % (float) 0.1;
        n_50c= (int) (change / 0.1);
        
        
        change = rem;
        rem = rem % (float) 0.5;
        n_10c= (int) (change / 0.5);
        
        change = rem;
        rem = rem % (float) 0.05;
        n_5c= (int) (change / 0.05);
        
        
        
        
System.out.printf("Disbursed as follows: %d x N$20; "
        + "%d x N$10; %d x N$5; %d x N$1; %d x "
        + "50c; %d x 10c; %d x 5c",n_20,n_10,n_5,n_1,n_50c,n_10c,n_5c);
        
        
        
        
        
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS