4) Create the following in a Java program.
Create a Scanner
Prompt the user to Receive the amount and declare the variable
double amount for scanner
Declare variable integer remaining amount which is equal to (int)
(amount*100)
Find the number of one dollars bt declaring the integer variable
which is equal to remaining amount divided by 100
Declare: remaining amount %=100
Find the number of quarters:
Declare integer variable number of quarters equal to remaining
amount divided by 25
Declare: remaining amount %=25
Find the number of dimes:
Declare integer variable number of dimes equal to the remaining
amount divided by 10
Declare: remaining amount %=10
Find the number of nickels:
Find the number of nickels in the remaining amount divided by
5
Declare: remaining amount %=5
Find the number of pennies in the remaining amount
Where declare variable integer number of pennies equals to
remaining amount
Display results:
System.out.println("Your amount " + amount + " consists of");
System.out.println(" " + numberOfDollars + (numberOfDollars == 1 ?
" dollar" : " dollars"));
Instead of dollars using same line as above display the rest:
Display the number of quarters
Display the number of dimes
Display the number of nickels
Display the number of pennies
Please upovte if you are able to understand this and if there is any query do mention it in the comment section.
CODE:
import java.util.Scanner;//importing the Scanner class from util package
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//creating object
of Scanner class
System.out.println("Enter
amount");
double amount =
sc.nextDouble();//taking input from the user
int remainingAmount = (int)(amount
* 100);//calculating remainingAmount and coverting to int
System.out.println("remainingAmount");
int numberOfDollars =
remainingAmount % 100;//calculating numberOfDollars
int numberOfQuarters =
remainingAmount % 25;//calculating numberOfQuarters
int numberOfDimes = (int)(amount %
10);//calculating numberOfDimes and coverting to int
int numberOfNickels = (int)(amount
% 5);//calculating numberOfNickels and coverting to int
int numberOfPennies =
remainingAmount;//calculating pennies
System.out.println("Your amount " +
amount + " consists of");
System.out.println(" " +
numberOfDollars + (numberOfDollars == 1 ? " dollar" : "
dollars"));
System.out.println(" " +
numberOfQuarters + (numberOfQuarters == 1 ? " quarters" : "
quarters"));
System.out.println(" " +
numberOfDimes + (numberOfDimes == 1 ? " dimes" : " dimes"));
System.out.println(" " +
numberOfNickels + (numberOfNickels == 1 ? " nickels" : "
nickels"));
System.out.println(" " +
numberOfPennies + (numberOfPennies == 1 ? " pennies" : "
pennies"));
}
}
OUTPUT:
If this was supposed to be done in any other way or something is required to be modified then please mention it in the comment section otherwise please upvote.
Get Answers For Free
Most questions answered within 1 hours.