Coin Math Conversion Issue - java

i'm currently coding a program that will convert pennies into the correct number of Twenties, tens, fives, dollars, quarters, dimes, nickels, and pennies. I've gotten the first part of the program correct but once i get to dollars my math is not outputting the correct amount. Any help?
import java.util.Scanner;
public class assignment1 {
public static void main(String[] args) {
Scanner inputReader = new Scanner(System.in);
int num, remainder;
System.out.print("Enter a value as a number of pennies: ");
num = inputReader.nextInt();
remainder = num % 2000;
System.out.println(num + " pennies is equal to:");
System.out.println((num / 2000) + " twenties");
System.out.println((remainder / 1000) + " tens");
System.out.println(((remainder % 1000) / 500) + " fives");
System.out.println((((remainder % 1000) % 500) / 100) + " dollars");
System.out.println(((((remainder % 1000) % 500) % 100) / 25) + " quarters");
System.out.println(((((remainder % 1000) % 500) % 100) % 25) / 10 + " dimes");
System.out.println((((((remainder % 1000) % 500) % 100) % 25) % 10) / 5 + " nickels");
System.out.println(((((((remainder % 1000) % 500) % 100) % 25) % 10) % 5) / 1 + " pennies");
}
}

Ok. I see what you've done. Let's start with
System.out.println((num / 2000) + "twenties");
System.out.println((remainder / 1000) + "tens");
System.out.println((remainder / 1000) % 500 + "fives");
System.out.println((((remainder / 1000) % 500) % 100) + "dollars");
The first line is correct. But you cannot use a simple remainder to keep a running tally. Also, num and remainder are terrible variable names. You can do something like this for a running tally,
System.out.print("Enter a value as a number of pennies: ");
int pennies = inputReader.nextInt();
System.out.println(pennies + " pennies is equal to:");
int twenties = pennies / 2000;
pennies -= twenties * 2000;
int tens = pennies / 1000;
pennies -= tens * 1000;
int fives = pennies / 500;
pennies -= fives * 500;
int dollars = pennies / 100;
pennies -= dollars * 100;
int quarters = pennies / 25;
pennies -= quarters * 25;
int dimes = pennies / 10;
pennies -= dimes * 10;
int nickels = pennies / 5;
pennies -= nickels * 5;
System.out.printf("%d twenties, %d tens, %d fives, %d dollars, "
+ "%d quarters, %d dimes, %d nickels, %d pennies%n", twenties,
tens, fives, dollars, quarters, dimes, nickels, pennies);
That would output (with 5869 as input),
5869 pennies is equal to:
2 twenties, 1 tens, 1 fives, 3 dollars, 2 quarters, 1 dimes, 1 nickels, 4 pennies
Or (if you wanted a non-running tally), just remove the subtractions from pennies like
System.out.println(pennies + " pennies is equal to:");
int twenties = pennies / 2000;
int tens = pennies / 1000;
int fives = pennies / 500;
int dollars = pennies / 100;
int quarters = pennies / 25;
int dimes = pennies / 10;
int nickels = pennies / 5;
System.out.printf("%d twenties, %d tens, %d fives, %d dollars, "
+ "%d quarters, %d dimes, %d nickels, %d pennies%n", twenties,
tens, fives, dollars, quarters, dimes, nickels, pennies);
And for 5869 you get
5869 pennies is equal to:
2 twenties, 5 tens, 11 fives, 58 dollars, 234 quarters, 586 dimes, 1173 nickels, 5869 pennies

This is possible with only mathematical operators. In most other programming task you'll encounter, you should use control structures to accomplish the steps in an algorithm.
Process
Begin with the number of pennies,
Divide by 2000 (the value of a $20-bill) and assign to a new variable,
Subtract from the number of pennies the value of the twenties,
Repeat for subsequent denominations ($10 bills, $5 bills, etc.)

I think the problem is that each time you divide your number it returns a new number as an Integer. So if you divide 100 pennies by 1000 it returns 0 so the rest of your math is going to return 0 no matter what you do.
example based on your code:
//assuming the input is 100 pennies the first remainder/1000 will equal to 0
System.out.println(((((((remainder / 1000) % 500) % 100) % 25) % 10) %5) % 1 + "pennies");
You haven't specified that you have to use modulus so i am assuming you're not.
Here's what i would do:
System.out.println(num + " pennies is equal to:");
System.out.println((num / 2000) + "twenties");
System.out.println((num / 1000) + "tens");
System.out.println((num / 500) + "fives");
System.out.println((num / 100) + "dollars");
System.out.println((num / 25) + "quarters");
System.out.println((num / 10) + "dimes");
System.out.println((num / 5) + "nickels");
System.out.println(num + "pennies");

Related

java code to convert cents into dimes, nickels

System.out.print("Enter the AMOUNT in CENTS: ");
int cents = scan.nextInt();
scan.close();
int dimes = (cents % 25)/10;
System.out.println("Dimes is " +dimes);
int nickels = ((cents % 25) %10) /5;
System.out.println("Nickels is " +nickels);
int pennies = ((cents % 25) %10) %5;
System.out.println("Pennies is " +pennies);
I can't seem to convert the cents into dimes, nickels, and pennies. the output should be like this:
Enter AMOUNT in CENTS : 28
Dimes is 2
Nickels is 1
Pennies is 3
but the output of my program is showing is:
Enter the AMOUNT in CENTS: 28
Enter the AMOUNT in CENTS: 28
Dimes is 0
Nickels is 0
Pennies is 3
can someone help me with the formula?
Try this:
int dimes = cents / 10;
System.out.println("Dimes is " +dimes);
int nickels = (cents % 10) / 5;
System.out.println("Nickels is " +nickels);
int pennies = (cents % 10) % 5;
System.out.println("Pennies is " +pennies);
A dime is 10 cents. A nickel is 5 cents. A penny is 1 cent.

How to display correct amount of change?

I am pretty new to java and am trying to create a simple program that converts an amount of money into change. However, I find that the program always prints one penny less than necessary.
I would have just added 1 to my pennies variable before printing but this would mean that when change is 0, it would still add a penny as change.
Here is my code:
change = amtPaid-Math.round((total*1.13)*100)/100.0;
pennies = (int) (change * 100);
twenties = (int)(pennies/2000);
pennies %= 2000;
tens = (int)(pennies/1000);
pennies %= 1000;
fives = (int)(pennies/500);
pennies %= 500;
toonies = (int)(pennies/200);
pennies %= 200;
loonies = (int)(pennies/100);
pennies %= 100;
quarters = (int)(pennies/25);
pennies %= 25;
dimes = (int)(pennies/10);
pennies %= 10;
nickels = (int)(pennies/5);
pennies %= 5;
I get this output after providing sample input:
The change will be: 1.83
To make up this amount, you will need:
0 twenty-dollar bills 0 ten-dollar bills
0 five-dollar bills 0 toonies
1 loonies 3 quarters
0 dimes 1 nickels
2 pennies
Does anyone know why this may be happening and how to fix it?
The way I see it, get your Grand Total (cost and taxes all in) to a decimal precision of no more than 2 (ie: 55.28). Don't allow the decimal precision to be more than 2. Whatever the rounding mechanism you use, make sure your happy with the final result.
Subtract the Amount Owed from the Amount Paid and make sure the final result is to a decimal precision of 2 (ie: 60.00 - 55.2814 = 4.7186 = 4.72 {rounded-up}). By not rounding properly to a decimal precision of 2 is where you can loose or gain a penny when calculating returned change.
Break the Amount To Return into Dollars and Cents. Both should be integer values (ie: 4 Dollars and 72 Cents).
// A value for the sake of this example. Taxes Included!
double totalOwed = 52.26534;
// Round to a precision of 2 (round UP on .5)
double grandTotal = BigDecimal.valueOf(totalOwed)
.setScale(2, RoundingMode.HALF_UP).doubleValue();
// A value for the sake of this example.
double amountPaid = 60.00;
// Round to a precision of 2 (round UP on .5)
double returnValue = BigDecimal.valueOf(amountPaid - grandTotal)
.setScale(2, RoundingMode.HALF_UP).doubleValue();
int dollars = Integer.parseInt(String.valueOf(returnValue).split("\\.")[0]);
int cents = Integer.parseInt(String.valueOf(returnValue).split("\\.")[1]);
Work out the bill currency to be returnd and add it to a List for display later. There are a LOT of different ways to do this. Here is one way:
List<String> change = new ArrayList<>();
int returnVal;
// Some displayable information
change.add("Total Cost: --> " + String.valueOf(grandTotal));
change.add("Total Paid: --> " + String.valueOf(amountPaid));
change.add("Return change: --> " + String.valueOf(returnValue));
// 100 Dollar Bills
if (dollars >= 100) {
returnVal = dollars / 100;
change.add(" " + String.valueOf(returnVal) + " Hundred Dollar Bill(s)");
dollars -= (returnVal * 100);
}
// 50 Dollar Bills
if (dollars >= 50) {
returnVal = dollars / 50;
change.add(" " + String.valueOf(returnVal) + " Fifty Dollar Bill(s)");
dollars -= (returnVal * 50);
}
// 20 Dollar Bills
if (dollars >= 20) {
returnVal = dollars / 20;
change.add(" " + String.valueOf(returnVal) + " Twenty Dollar Bill(s)");
dollars -= (returnVal * 20);
}
// 10 Dollar Bills
if (dollars >= 10) {
returnVal = dollars / 10;
change.add(" " + String.valueOf(returnVal) + " Ten Dollar Bill(s)");
dollars -= (returnVal * 10);
}
// 5 Dollar Bills
if (dollars >= 5) {
returnVal = dollars / 5;
change.add(" " + String.valueOf(returnVal) + " Five Dollar Bill(s)");
dollars -= (returnVal * 5);
}
// 2 Dollar Bills
if (dollars >= 2) {
returnVal = dollars / 2;
change.add(" " + String.valueOf(returnVal) + " Two Dollar Bill(s)");
dollars -= (returnVal * 2);
}
// 1 Dollar Bills
if (dollars >= 1) {
returnVal = dollars / 1;
change.add(" " + String.valueOf(returnVal) + " One Dollar Bill(s)");
dollars -= (returnVal * 1);
}
// Quarters (working against cents now)
if (cents >= 25) {
returnVal = cents / 25;
change.add(" " + String.valueOf(returnVal) + " Quarter(s)");
cents -= (returnVal * 25);
}
// Dimes
if (cents >= 10) {
returnVal = cents / 10;
change.add(" " + String.valueOf(returnVal) + " Dime(s)");
cents -= (returnVal * 10);
}
// Nickles
if (cents >= 5) {
returnVal = cents / 5;
change.add(" " + String.valueOf(returnVal) + " Nickle(s)");
cents -= (returnVal * 5);
}
// Pennies
if (cents >= 1) {
returnVal = cents / 1;
change.add(" " + String.valueOf(returnVal) + " Pennies");
cents -= (returnVal * 1);
}
// Display the Change-Back in Console Window
for (String str : change) {
System.out.println(str);
}
As you can see there is a lot of repetitious code in the example above. You can get rid of this by doing it yet another way. You can utilize an Array or Collection mechanism to hold the Dollar and coin denominations, for example:
// A value for the sake of this example. Taxes Included!
double totalOwed = 52.26534;
// Round to a precision of 2 (round UP on .5)
double grandTotal = BigDecimal.valueOf(totalOwed)
.setScale(2, RoundingMode.HALF_UP).doubleValue();
// A value for the sake of this example.
double amountPaid = 60.00;
// Round to a precision of 2 (round UP on .5)
double returnValue = BigDecimal.valueOf(amountPaid - grandTotal)
.setScale(2, RoundingMode.HALF_UP).doubleValue();
int dollars = Integer.parseInt(String.valueOf(returnValue).split("\\.")[0]);
int cents = Integer.parseInt(String.valueOf(returnValue).split("\\.")[1]);
List<String> change = new ArrayList<>();
int returnVal;
// Using arrays (or a collection mechanism) like this
// allows you to set whatever denominations you like.
String[][] dollarDenominations = {
{"100", " Hundred Dollar Bill(s)"},
{"50" , " Fifty Dollar Bill(s)" },
{"20" , " Twenty Dollar Bill(s)" },
{"10" , " Ten Dollar Bill(s)" },
{"5" , " Five Dollar Bill(s)" },
{"2" , " Two Dollar Bill(s)" },
{"1" , " One Dollar Bill(s)" }
};
// Although not commonly used, you might even want to
// add 50 cent pieces to the Array.
String[][] coinDenominations = {
{"25", " Quarter(s)"},
{"10", " Dime(s)" },
{"5" , " Nickle(s)" },
{"1" , " Pennies" }
};
// Some displayable information
change.add("Total Cost: --> " + String.valueOf(grandTotal));
change.add("Total Paid: --> " + String.valueOf(amountPaid));
change.add("Return change: --> " + String.valueOf(returnValue));
// Dollar Denominations
for (int i = 0; i < dollarDenominations.length; i++) {
int denom = Integer.parseInt(dollarDenominations[i][0]);
String typeString = dollarDenominations[i][1];
if (dollars >= denom) {
returnVal = dollars / denom;
change.add(" " + String.valueOf(returnVal) + typeString);
dollars -= (returnVal * denom);
}
}
// Coin Denominations
for (int i = 0; i < coinDenominations.length; i++) {
int denom = Integer.parseInt(coinDenominations[i][0]);
String typeString = coinDenominations[i][1];
if (cents >= denom) {
returnVal = cents / denom;
change.add(" " + String.valueOf(returnVal) + typeString);
cents -= (returnVal * denom);
}
}
// Display the Change-Back data in Console Window...
for (String str : change) {
System.out.println(str);
}
As said before, there are several other ways to do this sort of thing. The important thing is to work against Change-Back values that are to 2 decimal places. After all...to my knowledge, there is no such denomination as a one-sixth penny (there is no denomination for mills). The US at one time (between 1793 and 1857) use to have a half-Penny but that no longer exists and never ever will again for obvious reasons. In Canada there is no Penny since it costs almost 2 cents to make one and ultimately (in my opinion) the United States will most likely follow this scheme and save millions upon millions in the process since it also costs approx. 1.82+ cents to make a US penny.

converting money into all possible combinations

I know my question is repeated but I still couldn't understand what the other answers are explaining. Below is my code and I have calculated it to get to the first line which is 57 pennies + 0 dimes + 0 nickels + 0 quarters and I am thinking to run a loop that will list all the possible combinations of pennies, dimes, nickels, quarters. But, I don't know how.
public class Conversion{
public static void main(String[] args) {
int cents = 57;
int quarter = 25;
int dime = 10;
int nickel = 5;
int penny = 1;
int totalPennies = cents / penny;
cents %= penny;
int totalNickels = cents / nickel;
cents %= nickel;
int totalDimes = cents / dime;
cents %= dime;
int totalQuarters = cents / quarter;
cents %= quarter;
System.out.print(totalPennies + " pennies + ");
System.out.print(totalNickels + " nickels + ");
System.out.print(totalDimes + " dimes + ");
System.out.println(totalQuarters + " quarters");
}
}
Your order of operations is backwards.
The first thing you do is count how many pennies you have. Since pennies are literally valued at 1, you can have 57 pennies and make up $0.57 just fine. That is obviously not what you want to accomplish.
What you want to do is count from your highest denomination and work your way backwards. Here's a sample.
// This should be the first operation
int totalQuarters = cents / quarter;
cents %= quarter;
I leave reordering the rest as an exercise for the reader, but the output then becomes correct once successfully reordered.
2 pennies + 1 nickels + 0 dimes + 2 quarters

Why won't my code divide the remaining cents by dimes, nickels, and pennies after it divided by quarters?

String choice = "y";
while(choice.equalsIgnoreCase("y")){
if (cents <= 99 && cents > 0){
quarters = cents / quarters;
System.out.println("Quarters: " + quarters);
dimes = cents / dimes;
System.out.println("Dimes: " + dimes);
nickels = cents / nickels;
System.out.println("Nickels: " + nickels);
pennies = cents / pennies;
System.out.println("Pennies: " + pennies);
Do i need to implement a modulus operator after each step?
Ah you have a can't see the wood for the trees problem.
Old school trick this called a dry run
Cents Quarters Dimes Nickels Pennies Remaining Cents
41 1 16
16 1 6
6 1 1
1 1 0
You are starting from 41 each calculation.

coins output in vending maching? [duplicate]

This question already has answers here:
Java program that tells what coins to give out for any amount of change from 1 cent to 99 cents
(3 answers)
Closed 7 years ago.
I'm not sure where I'm wrong. I am not being able to get the proper output as expected. Below is my piece of program:
public static void insertMoney(){
double dollars, quarters, dimes, nickels, pennies;
double paymentSum=0;
System.out.println("Enter amount of money in cents.");
Scanner input = new Scanner(System.in);
paymentSum = input.nextDouble();
//paymentSum = (paymentSum)/100;
if (paymentSum <= 0){
System.out.println("Please feed me more money, so I can feed you!. Try again next time :)");
return;
}
System.out.println("You entered: " +paymentSum+" Cent(s).");
System.out.println((paymentSum/100) + " Cent(s) in coins can be given as: ");
dollars = paymentSum /100;
paymentSum = paymentSum%100;
quarters = paymentSum/25;
paymentSum = paymentSum % 25;
dimes = paymentSum/10;
paymentSum = paymentSum % 10;
nickels = paymentSum/5;
paymentSum = paymentSum % 5;
pennies = paymentSum;
System.out.println(dollars + " dollar(s)");
System.out.println(quarters + " quarter(s)");
System.out.println(dimes + " dime(s)");
System.out.println(nickels + " nickel(s) and");
System.out.println(pennies + " pennies");
}
I'm trying to get output as this:
You entered: 26.0 Cent(s).
0.26 Dollar(s) in coins can be given as:
0.0 dollar(s)
1.0 quarter(s)
0.0 dime(s)
0.0 nickel(s) and
1.0 pennies
But I get this:
You entered: 26.0 Cents(s).
0.26 Dollar(s) in coins can be given as:
0.26 dollar(s)
1.04 quarter(s)
0.1 dime(s)
0.2 nickel(s) and
1.0 pennies
What am I doing wrong in my code? Any help would be appreciated. Thanks!
The problem is your expecting integer division but using a double paymentSum.
As noted in this article, since paymentSum is a double, any division will also produce a double. That's why your quarters are showing up as 1.04 instead of 1, and why your dimes are 0.1 instead of 0, etc...

Categories

Resources