Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class LabProgram {
- public static void main(String[] args) {
- // define variables
- Scanner scnr = new Scanner(System.in);
- int penniesInput;
- int pennies = 0;
- int nickels = 0;
- int dimes = 0;
- int quarters = 0;
- int dollars = 0;
- // ask user for input
- penniesInput = scnr.nextInt();
- // first, we simply check if input is 0, if so, end the program
- // with a message. if not, continue the program.
- if (penniesInput == 0) {
- System.out.println("No change");
- scnr.close();
- return;
- }
- // the way to think about this is to start from the highest coin and work
- // downwards to the lowest coin, so dollars to pennies. so the first question
- // is how many dollars does that represent, if any?
- // well, we know 100 pennies = 1 dollar, but the question here is how many times
- // can you fit 100 in that input. ex: input is 200, i can fit 2 times 100, so 2 dollars,
- // if input is 50, i can fit 0 times 100 in it, etc. we need to determine this first,
- // it will tell us how many dollars and also, if remainder is 0, there is no need to proceed
- // further, we reached 0 pennies remaining to distribute.
- // so, what can you use to figure out how many times does 100 fit in the number?
- // well, lucky for us, Java does the job for us, kinda, i mean if we declared the
- // variables as ints, then if there is a remainder, it will be cut and only whole
- // ints are considered, how many of them CAN fit. so, we can divide the input by 100
- // to find how many whole INTs does fit in the number, and this is our dollar variable.
- dollars = penniesInput / 100;
- // then, we need to subtract that many dollars from the input to continue forward,
- // so dollars(int) * 100
- penniesInput = penniesInput - (100 * dollars); // remaining pennies after this operation
- // from here, we need to check if you have 0 or you can continue.
- // if you cannot continue, because it is 0, then you end the program here,
- // there is no need to continue forward. if not, continue the program till
- // you reach the end, pennies. if program continues, we still keep track
- // of the info, stored in the variables.
- if (penniesInput == 0) {
- // check if 1 dollar or more for the plural case
- if (dollars > 1) {
- System.out.println(dollars + " Dollars");
- } else {
- System.out.println(dollars + " Dollar");
- }
- // after that, close scanner and end the program
- scnr.close();
- return;
- }
- // from here, we use the same format of logic as above, but now instead
- // of dealing with 100, we deal with other numbers that represent quarters,
- // dimes, nickels and pennies, so 25, 10, 5 and 1. the only added tricky part
- // is that for whatever part you are on and need to end the program, you need
- // to keep track of what you had before, stored in variables and print correctly
- // from here, im not going to write a lot of comments, i will just make a compact
- // form of what you need to proceed for quarters, dimes, nickels and pennies.
- // it's literally the same format, just other numbers
- // let's do quarters next, so instead of dealing with 100, we deal with 25
- quarters = penniesInput / 25;
- penniesInput = penniesInput - (25 * quarters);
- if (penniesInput == 0) {
- // we follow the order of prints, dollars first, then quarters, etc
- // highest to lowest
- if (dollars > 1) {
- System.out.println(dollars + " Dollars");
- } else if (dollars == 1) {
- System.out.println(dollars + " Dollar");
- }
- if (quarters > 1) {
- System.out.println(quarters + " Quarters");
- } else {
- System.out.println(quarters + " Quarter");
- }
- scnr.close();
- return;
- }
- // next is dimes, so we deal with 10
- dimes = penniesInput / 10;
- penniesInput = penniesInput - (10 * dimes);
- if (penniesInput == 0) {
- if (dollars > 1) {
- System.out.println(dollars + " Dollars");
- } else if (dollars == 1) {
- System.out.println(dollars + " Dollar");
- }
- if (quarters > 1) {
- System.out.println(quarters + " Quarters");
- } else if (quarters == 1) {
- System.out.println(quarters + " Quarter");
- }
- if (dimes > 1) {
- System.out.println(dimes + " Dimes");
- } else {
- System.out.println(dimes + " Dime");
- }
- scnr.close();
- return;
- }
- // then, we have nickels, so we deal with 5 now
- nickels = penniesInput / 5;
- penniesInput = penniesInput - (5 * nickels);
- if (penniesInput == 0) {
- if (dollars > 1) {
- System.out.println(dollars + " Dollars");
- } else if (dollars == 1) {
- System.out.println(dollars + " Dollar");
- }
- if (quarters > 1) {
- System.out.println(quarters + " Quarters");
- } else if (quarters == 1) {
- System.out.println(quarters + " Quarter");
- }
- if (dimes > 1) {
- System.out.println(dimes + " Dimes");
- } else if (dimes == 1) {
- System.out.println(dimes + " Dime");
- }
- if (nickels > 1) {
- System.out.println(nickels + " Nickels");
- } else {
- System.out.println(nickels + " Nickel");
- }
- scnr.close();
- return;
- }
- // finally, we deal with pennies, so instead of 5, it's 1, the lowest coin
- // at this point, the program ends no matter what, cause it's the lowest
- // we can go anyway
- pennies = penniesInput / 1;
- penniesInput = penniesInput - (1 * pennies);
- if (penniesInput == 0) {
- if (dollars > 1) {
- System.out.println(dollars + " Dollars");
- } else if (dollars == 1) {
- System.out.println(dollars + " Dollar");
- }
- if (quarters > 1) {
- System.out.println(quarters + " Quarters");
- } else if (quarters == 1) {
- System.out.println(quarters + " Quarter");
- }
- if (dimes > 1) {
- System.out.println(dimes + " Dimes");
- } else if (dimes == 1) {
- System.out.println(dimes + " Dime");
- }
- if (nickels > 1) {
- System.out.println(nickels + " Nickels");
- } else if (nickels == 1) {
- System.out.println(nickels + " Nickel");
- }
- if (pennies > 1) {
- System.out.println(pennies + " Pennies");
- } else {
- System.out.println(pennies + " Penny");
- }
- scnr.close();
- return;
- }
- scnr.close(); // always close scanner
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement