Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

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
Check Mark
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.

Knowledge Booster
Background pattern image
Computer Science
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.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education