• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Feb 21st, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import java.util.Scanner;
2.
3. public class LabProgram {
4.     public static void main(String[] args) {
5.
6.         // define variables
7.         Scanner scnr = new Scanner(System.in);
8.         int penniesInput;
9.         int pennies = 0;
10.         int nickels = 0;
11.         int dimes = 0;
12.         int quarters = 0;
13.         int dollars = 0;
14.
15.         // ask user for input
16.         penniesInput = scnr.nextInt();
17.
18.         // first, we simply check if input is 0, if so, end the program
19.         // with a message. if not, continue the program.
20.         if (penniesInput == 0) {
21.             System.out.println("No change");
22.             scnr.close();
23.             return;
24.         }
25.
26.         // the way to think about this is to start from the highest coin and work
27.         // downwards to the lowest coin, so dollars to pennies. so the first question
28.         // is how many dollars does that represent, if any?
29.
30.         // well, we know 100 pennies = 1 dollar, but the question here is how many times
31.         // can you fit 100 in that input. ex: input is 200, i can fit 2 times 100, so 2 dollars,
32.         // if input is 50, i can fit 0 times 100 in it, etc. we need to determine this first,
33.         // it will tell us how many dollars and also, if remainder is 0, there is no need to proceed
34.         // further, we reached 0 pennies remaining to distribute.
35.
36.         // so, what can you use to figure out how many times does 100 fit in the number?
37.         // well, lucky for us, Java does the job for us, kinda, i mean if we declared the
38.         // variables as ints, then if there is a remainder, it will be cut and only whole
39.         // ints are considered, how many of them CAN fit. so, we can divide the input by 100
40.         // to find how many whole INTs does fit in the number, and this is our dollar variable.
41.
42.         dollars = penniesInput / 100;
43.
44.         // then, we need to subtract that many dollars from the input to continue forward,
45.         // so dollars(int) * 100
46.
47.         penniesInput = penniesInput - (100 * dollars); // remaining pennies after this operation
48.
49.         // from here, we need to check if you have 0 or you can continue.
50.         // if you cannot continue, because it is 0, then you end the program here,
51.         // there is no need to continue forward. if not, continue the program till
52.         // you reach the end, pennies. if program continues, we still keep track
53.         // of the info, stored in the variables.
54.
55.         if (penniesInput == 0) {
56.             // check if 1 dollar or more for the plural case
57.             if (dollars > 1) {
58.                 System.out.println(dollars + " Dollars");
59.             } else {
60.                 System.out.println(dollars + " Dollar");
61.             }
62.             // after that, close scanner and end the program
63.             scnr.close();
64.             return;
65.         }
66.
67.         // from here, we use the same format of logic as above, but now instead
68.         // of dealing with 100, we deal with other numbers that represent quarters,
69.         // dimes, nickels and pennies, so 25, 10, 5 and 1. the only added tricky part
70.         // is that for whatever part you are on and need to end the program, you need
71.         // to keep track of what you had before, stored in variables and print correctly
72.
73.         // from here, im not going to write a lot of comments, i will just make a compact
74.         // form of what you need to proceed for quarters, dimes, nickels and pennies.
75.         // it's literally the same format, just other numbers
76.
77.         // let's do quarters next, so instead of dealing with 100, we deal with 25
78.         quarters = penniesInput / 25;
79.         penniesInput = penniesInput - (25 * quarters);
80.         if (penniesInput == 0) {
81.             // we follow the order of prints, dollars first, then quarters, etc
82.             // highest to lowest
83.             if (dollars > 1) {
84.                 System.out.println(dollars + " Dollars");
85.             } else if (dollars == 1) {
86.                 System.out.println(dollars + " Dollar");
87.             }
88.             if (quarters > 1) {
89.                 System.out.println(quarters + " Quarters");
90.             } else {
91.                 System.out.println(quarters + " Quarter");
92.             }
93.             scnr.close();
94.             return;
95.         }
96.
97.         // next is dimes, so we deal with 10
98.         dimes = penniesInput / 10;
99.         penniesInput = penniesInput - (10 * dimes);
100.         if (penniesInput == 0) {
101.             if (dollars > 1) {
102.                 System.out.println(dollars + " Dollars");
103.             } else if (dollars == 1) {
104.                 System.out.println(dollars + " Dollar");
105.             }
106.             if (quarters > 1) {
107.                 System.out.println(quarters + " Quarters");
108.             } else if (quarters == 1) {
109.                 System.out.println(quarters + " Quarter");
110.             }
111.             if (dimes > 1) {
112.                 System.out.println(dimes + " Dimes");
113.             } else {
114.                 System.out.println(dimes + " Dime");
115.             }
116.             scnr.close();
117.             return;
118.         }
119.
120.         // then, we have nickels, so we deal with 5 now
121.         nickels = penniesInput / 5;
122.         penniesInput = penniesInput - (5 * nickels);
123.         if (penniesInput == 0) {
124.             if (dollars > 1) {
125.                 System.out.println(dollars + " Dollars");
126.             } else if (dollars == 1) {
127.                 System.out.println(dollars + " Dollar");
128.             }
129.             if (quarters > 1) {
130.                 System.out.println(quarters + " Quarters");
131.             } else if (quarters == 1) {
132.                 System.out.println(quarters + " Quarter");
133.             }
134.             if (dimes > 1) {
135.                 System.out.println(dimes + " Dimes");
136.             } else if (dimes == 1) {
137.                 System.out.println(dimes + " Dime");
138.             }
139.             if (nickels > 1) {
140.                 System.out.println(nickels + " Nickels");
141.             } else {
142.                 System.out.println(nickels + " Nickel");
143.             }
144.             scnr.close();
145.             return;
146.         }
147.
148.         // finally, we deal with pennies, so instead of 5, it's 1, the lowest coin
149.         // at this point, the program ends no matter what, cause it's the lowest
150.         // we can go anyway
151.         pennies = penniesInput / 1;
152.         penniesInput = penniesInput - (1 * pennies);
153.         if (penniesInput == 0) {
154.             if (dollars > 1) {
155.                 System.out.println(dollars + " Dollars");
156.             } else if (dollars == 1) {
157.                 System.out.println(dollars + " Dollar");
158.             }
159.             if (quarters > 1) {
160.                 System.out.println(quarters + " Quarters");
161.             } else if (quarters == 1) {
162.                 System.out.println(quarters + " Quarter");
163.             }
164.             if (dimes > 1) {
165.                 System.out.println(dimes + " Dimes");
166.             } else if (dimes == 1) {
167.                 System.out.println(dimes + " Dime");
168.             }
169.             if (nickels > 1) {
170.                 System.out.println(nickels + " Nickels");
171.             } else if (nickels == 1) {
172.                 System.out.println(nickels + " Nickel");
173.             }
174.             if (pennies > 1) {
175.                 System.out.println(pennies + " Pennies");
176.             } else {
177.                 System.out.println(pennies + " Penny");
178.             }
179.             scnr.close();
180.             return;
181.         }
182.
183.         scnr.close(); // always close scanner
184.
185.     }
186. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top