Write a program that repeatedly reads an integer between 0 a

Write a program that repeatedly reads an integer between 0 and 100 that represents a number of cents. Convert that number of cents to the equivalent number of quarters, dimes, nickels, and pennies. The program should output the maximum number of quarters that will fit, then the maximum number of dimes that will fit into what then remains, and so on. Then the program will ask for the next amount. If the amount is greater than 100 write an error message and then ask for the next amount of cents. If the amount is negative, the program should quit. For example: Notice that this is a sentinel-controlled loop. The program Average. java on page 231 in the text is a helpful example. Calculate the output values by using the integer division operator and the integer remainder operator. You will probably find it convenient to use variables to hold the number of quarters, dimes, nickels, and pennies. Calculate them in that order.

Solution

Average.java

import java.util.Scanner;


public class Average {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       while(true){
       System.out.print(\"Enter the number of cents: \");
       int cents = scan.nextInt();
       if(cents == -1){
           System.out.println(\"Bye\");
           break;
       }
       else if(cents < 0 || cents >100){
           System.out.println(\"Please enter a value 0 to 100\");
       }
       else{
           int quarters = cents / 25;
           cents = cents % 25;
           int dimes = cents/ 10;
           cents = cents % 10;
           int nickels = cents / 5;
           cents = cents % 5;
           int penny = cents;
           System.out.println(\"Number of quarters: \"+quarters);
           System.out.println(\"Number of dimes: \"+dimes);
           System.out.println(\"Number of nickels: \"+nickels);
           System.out.println(\"Number of pennies: \"+penny);
       }
       }
   }

}

Output:

Enter the number of cents: 67
Number of quarters: 2
Number of dimes: 1
Number of nickels: 1
Number of pennies: 2
Enter the number of cents: 132
Please enter a value 0 to 100
Enter the number of cents: 43
Number of quarters: 1
Number of dimes: 1
Number of nickels: 1
Number of pennies: 3
Enter the number of cents: -1
Bye

 Write a program that repeatedly reads an integer between 0 and 100 that represents a number of cents. Convert that number of cents to the equivalent number of
 Write a program that repeatedly reads an integer between 0 and 100 that represents a number of cents. Convert that number of cents to the equivalent number of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site