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