Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
Last active September 15, 2023 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigamonkey/a1572aa32b68285a6fe324a0d75f45cc to your computer and use it in GitHub Desktop.
Save gigamonkey/a1572aa32b68285a6fe324a0d75f45cc to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Change0 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Item 1 price: ");
double price1 = input.nextDouble();
System.out.print("Item 2 price: ");
double price2 = input.nextDouble();
System.out.print("Item 3 price: ");
double price3 = input.nextDouble();
double total = price1 + price2 + price3;
System.out.printf("Total is %.02f\n", total);
System.out.print("Enter amount paid: ");
double paid = input.nextDouble();
double owed = paid - total;
System.out.printf("Change owed: %.02f\n", owed);
int twenties = (int) (owed / 20);
owed -= twenties * 20;
int tens = (int) (owed / 10);
owed -= tens * 10;
int fives = (int) (owed / 5);
owed -= fives * 5;
int ones = (int) (owed / 1);
owed -= ones;
int quarters = (int) (owed / 0.25);
owed -= quarters * 0.25;
int dimes = (int) (owed / 0.10);
owed -= dimes * 0.10;
int nickels = (int) (owed / 0.05);
owed -= nickels * 0.05;
int pennies = (int) (owed / 0.01);
owed -= pennies * 0.01;
System.out.printf("%d twenties; %d tens; %d fives; %d ones\n", twenties, tens, fives, ones);
System.out.printf("%d quarters; %d dimes; %d nickels; %d pennies\n", quarters, dimes, nickels, pennies);
System.out.printf("Owed: %.16f%n", owed);
}
}
import java.util.Scanner;
public class Change1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Item 1 price: ");
double price1 = input.nextDouble();
System.out.print("Item 2 price: ");
double price2 = input.nextDouble();
System.out.print("Item 3 price: ");
double price3 = input.nextDouble();
double total = price1 + price2 + price3;
System.out.printf("Total is %.02f\n", total);
System.out.print("Enter amount paid: ");
double paid = input.nextDouble();
double owed = paid - total;
System.out.printf("Change owed: %.02f\n", owed);
int twenties = (int) (owed / 20);
owed %= 20;
int tens = (int) (owed / 10);
owed %= 10;
int fives = (int) (owed / 5);
owed %= 10;
int ones = (int) (owed / 1);
owed %= 1;
int quarters = (int) (owed / 0.25);
owed %= 0.25;
int dimes = (int) (owed / 0.10);
owed %= 0.10;
int nickels = (int) (owed / 0.05);
owed %= 0.05;
int pennies = (int) (owed / 0.01);
owed %= 0.01;
System.out.printf("%d twenties; %d tens; %d fives; %d ones\n", twenties, tens, fives, ones);
System.out.printf("%d quarters; %d dimes; %d nickels; %d pennies\n", quarters, dimes, nickels, pennies);
System.out.printf("Owed: %.16f%n", owed);
}
}
import java.util.Scanner;
public class Change2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Item 1 price: ");
int price1 = (int)(input.nextDouble() * 100);
System.out.print("Item 2 price: ");
int price2 = (int)(input.nextDouble() * 100);
System.out.print("Item 3 price: ");
int price3 = (int)(input.nextDouble() * 100);
int total = price1 + price2 + price3;
System.out.printf("Total is %.02f%n", total/100.0);
System.out.print("Enter amount paid: ");
int paid = (int)(input.nextDouble() * 100);
int owed = paid - total;
System.out.printf("Change owed: %.02f%n", owed/100.0);
int twenties = owed / 2000;
owed %= 2000;
int tens = owed / 1000;
owed %= 1000;
int fives = owed / 500;
owed %= 500;
int ones = owed / 100;
owed %= 100;
int quarters = owed / 25;
owed %= 25;
int dimes = owed / 10;
owed %= 10;
int nickels = owed / 5;
owed %= 5;
int pennies = owed / 1;
owed %= 1;
System.out.printf("%d twenties; %d tens; %d fives; %d ones%n", twenties, tens, fives, ones);
System.out.printf("%d quarters; %d dimes; %d nickels; %d pennies%n", quarters, dimes, nickels, pennies);
System.out.printf("Owed: %d%n", owed);
}
}
123.45
345.67
321.99
12345.68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment