import java.util.Scanner; public class MakeChange { public static void main(String[] args) { // Create a Scanner object for user input Scanner scanner = new Scanner(System.in); // Ask the user for the amount of change in dollars System.out.print("Enter the amount of change: "); double dollarAmount = scanner.nextDouble(); // Convert the dollar amount to cents int cents = (int) (dollarAmount * 100); // Calculate the total number of coins int quarters = cents / 25; cents %= 25; int dimes = cents / 10; cents %= 10; int nickels = cents / 5; cents %= 5; int pennies = cents / 1; cents %= 1; // Calculate the total number of coins int totalCoins = quarters + dimes + nickels + pennies; System.out.println("Change due is: $"+ dollarAmount); // Output the total number of coins System.out.println("This is following "+ totalCoins+" coins: "); // Output the individual coin counts System.out.println("Quarters: " + quarters); System.out.println("Dimes: " + dimes); System.out.println("Nickels: " + nickels); System.out.println("Pennies: " + pennies); // Close the scanner scanner.close(); } } when i run the program it doesn't count 4 pennies when i enter 0.29, why is that?
import java.util.Scanner; public class MakeChange { public static void main(String[] args) { // Create a Scanner object for user input Scanner scanner = new Scanner(System.in); // Ask the user for the amount of change in dollars System.out.print("Enter the amount of change: "); double dollarAmount = scanner.nextDouble(); // Convert the dollar amount to cents int cents = (int) (dollarAmount * 100); // Calculate the total number of coins int quarters = cents / 25; cents %= 25; int dimes = cents / 10; cents %= 10; int nickels = cents / 5; cents %= 5; int pennies = cents / 1; cents %= 1; // Calculate the total number of coins int totalCoins = quarters + dimes + nickels + pennies; System.out.println("Change due is: $"+ dollarAmount); // Output the total number of coins System.out.println("This is following "+ totalCoins+" coins: "); // Output the individual coin counts System.out.println("Quarters: " + quarters); System.out.println("Dimes: " + dimes); System.out.println("Nickels: " + nickels); System.out.println("Pennies: " + pennies); // Close the scanner scanner.close(); } } when i run the program it doesn't count 4 pennies when i enter 0.29, why is that?
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
import java.util.Scanner;
public class MakeChange {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Ask the user for the amount of change in dollars
System.out.print("Enter the amount of change: ");
double dollarAmount = scanner.nextDouble();
// Convert the dollar amount to cents
int cents = (int) (dollarAmount * 100);
// Calculate the total number of coins
int quarters = cents / 25;
cents %= 25;
int dimes = cents / 10;
cents %= 10;
int nickels = cents / 5;
cents %= 5;
int pennies = cents / 1;
cents %= 1;
// Calculate the total number of coins
int totalCoins = quarters + dimes + nickels + pennies;
System.out.println("Change due is: $"+ dollarAmount);
// Output the total number of coins
System.out.println("This is following "+ totalCoins+" coins: ");
// Output the individual coin counts
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
System.out.println("Pennies: " + pennies);
// Close the scanner
scanner.close();
}
}
when i run the program it doesn't count 4 pennies when i enter 0.29, why is that?
Expert Solution
Step 1: Introduction to the cause of the error
The issue you're encountering in your program is related to floating-point precision when converting dollar amounts to cents. Floating-point numbers like double
in Java can sometimes lead to small precision errors, which can affect the results of calculations. When you enter 0.29 dollars, the conversion to cents may not yield exactly 29 cents due to these precision issues.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education