195k views
4 votes
1. Write a function that will ask the user to enter a value in dollars. The function should calculate and display the equivalent number of pennies, nickels, dimes, and quarters. Acceptable values are those which when multiplied by 100 will be exactly divisible by 50 (100*value must be exactly divisible by 50). Write a caller main function to call your function.

1 Answer

4 votes

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

valueInDollars();

}

public static void valueInDollars() {

double currentMoney, quarters, dimes, nickels, pennies;

Scanner input = new Scanner(System.in);

System.out.print("Enter a value in dollars: ");

currentMoney = input.nextDouble();

currentMoney *= 100;

quarters = (int)currentMoney / 25;

currentMoney = currentMoney % 25;

dimes = (int)currentMoney / 10;

currentMoney = currentMoney % 10;

nickels = (int)currentMoney / 5;

currentMoney = currentMoney % 5;

pennies = currentMoney;

System.out.print("Quarters: " + quarters + "\\" + "Dimes: " + dimes + "\\" + "Nickels: " + nickels + "\\" + "Pennies: " + pennies);

}

}

Step-by-step explanation:

Inside the function:

- Declare the variables

- Ask the user for a value in dollars

- Multiply that value by 100

- Find the amount of quarters, dimes, nickels, and pennies

- Print the values

Inside the main:

- Call the function

User Vbraun
by
3.7k points