java code to convert cents into dimes, nickels - java

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.

Related

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...

Coin Math Conversion Issue

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");

Java cash register: Getting change from in different subunits

Teaching myself Java, I reached an example problem that asks me to create a cash register program that will calculate the total change back to the customer in Dollars, Nickels, Quarters, Dimes, and pennies.
I worked out the user input but getting the correct sub-units is tricky part. For example: it's was easy to write the line of code that returned $1 dollar back if the cost was $4 and amount tendered was $5. But the trouble I am having is putting how many dimes and pennies needed in the correct field. ex2: total cost $4.62, tendered: $5, change: $0.38
System.out.print("Enter the sale amount: $ ");
double sale = user.nextDouble();
System.out.print("Enter the amount tendered by customer: $ ");
double tendered = user.nextDouble();
double totalChange = (tendered - sale);
System.out.printf("TOTAL CHANGE: $ %.2f\n", totalChange);
dollars = totalChange/1;
quarters = totalChange/25;
dimes = totalChange/10;
nickels = totalChange/5;
pennies = totalChange/1;
System.out.printf("DOLLARS: %.2f\n", dollars);
System.out.printf("QUARTERS: %.2f\n", quarters);
System.out.printf("DIMES: %.2f\n", dimes);
System.out.printf("NICKELS: %.2f\n", nickels);
System.out.printf("PENNIES: %.2f\n", pennies);
I don't need the answer (because I want to learn and teach myself), but just need some advice on how to go about doing so.
EDIT: What am I doing wrong with this algorithm:
dollars = totalChange/1;
dollars = (int)dollars;
dl = totalChange % 1;
quarters = dl/0.25;
q = quarters % 0.25;
quarters = (int)quarters;
dimes = q / .10;
d = dimes % .10;
nickels = d / .5;
n = nickels % .5;
pennies = n / .1;
n = pennies/ .1;
You are on the right track. After determining the largest number of whole dollars in change, subtract it. Then determine the largest number of quarters you can get from what is left. Repeat for each. It's the same way you do it yourself as a human.

Categories

Resources