Hello I am currently working with Java. I have a problem that I need to create a
ID: 3665943 • Letter: H
Question
Hello I am currently working with Java. I have a problem that I need to create a program for that goes like this :
Homework Project 3: Write an application that prompts for and reads a double value representing a monetary amount. Then determine the least number of each bill and coin needed to represent that amount, starting with the highest (assume that a twenty-dollar bill is the maximum size needed). For example, if the value entered is 47.63 (forty-seven dollars and sixty-three cents), then the output from the program should print the equivalent amount as
$ 47.63 is equivalent to :
2 twenty dollar bills
0 ten dollar bills
1 five dollar bills
2 one dollar bills
2 quarters
1 dimes
0 nickels
3 pennies
I cannot use anything I have not learned yet. I have only learned simple math operaters, currencyinstance, scanner input, and the different types. I cannot use if statements or loops and I am supposed to use a double value. My question is more of a statement I truly do not know where to begin.
thank you in advance.
Explanation / Answer
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter amount : ");
double val=sc.nextDouble(); // reading double value
String[] s=String.valueOf(val).split("\."); // splitting around '.'
// lets for example amount = 47.63
int integeral = Integer.parseInt(s[0]); // getting integeral part, 47
int fraction = Integer.parseInt(s[1]); // getting fractional part 63
// getting number of twenty dollar bills
int num_20 = integeral/20;
integeral = integeral%20; // getting rest amount
System.out.println(num_20 +" twenty dollar bills");
//getting number of ten dollor bills
int num_10 = integeral/10;
integeral = integeral%10;
System.out.println(num_10 +" ten dollar bills");
//getting number of 5 dollor bills
int num_5 = integeral/5;
integeral = integeral%5;
System.out.println(num_5 +" five dollar bills");
//getting number of 1 dollor bills
int num_1 = integeral;
System.out.println(integeral +" 1 dollar bills");
// doing operation with fraction part
int quarter = fraction/25; // 4 quarter= 1 dollar
fraction = fraction%25;
System.out.println(quarter+" quarters");
int nickel = fraction/20;
fraction = fraction%20;
System.out.println(nickel+" nickels");
int dimes = fraction/10;
fraction = fraction%10;
System.out.println(dimes+" dimes");
int pennies = fraction;
System.out.println(pennies+" pennies");
}
}
/*
OUTPUT:
Enter amount :
47.63
2 twenty dollar bills
0 ten dollar bills
1 five dollar bills
2 1 dollar bills
2 quarters
0 nickels
1 dimes
3 pennies
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.