Question

In: Computer Science

1. [10 marks] (Changes.java) Write code that asks the user to enter an amount of money...

1. [10 marks] (Changes.java) Write code that asks the user to enter an amount of money from 0 to 99 cents. The program then calculates and displays the number of coins from each denomination: quarters (25 cents), dimes (10 cents), nickels (5 cents), and cents. For example, if the user enters 93, your program should display There are 3 quarters, 1 dimes, 1 nickels, and 3 cents in 93 cents Note: You must use integer division and modular division to find the digits.

Solutions

Expert Solution

import java.util.Scanner;

public class Change{
        public static void main(String[] args) {
                int amount=0,d=0,q=0,n=0,p=0,di=0;
                  System.out.printf("Enter amount: ");
                  Scanner sc = new Scanner(System.in);
                  amount=sc.nextInt();
                  if(amount==0){
                    System.out.printf("no change");
                    return;
                  }
                  //finding dollors
                  if(amount>=100){
                    d=amount/100;
                    amount=amount%100;
                  }
                  //finding quarters
                  if(amount>=25){
                    q=amount/25;
                    amount=amount%25;
                  }
                  //findigng dimes
                  if(amount>=10){
                    di=amount/10;
                    amount=amount%10;
                  }
                  //finding nickels
                  if(amount>=5){
                    n=amount/5;
                    amount=amount%5;
                  }
                  p=amount;
                  if(d==1)
                    System.out.printf("%d dollor\n",d);
                  else if(d!=0)
                    System.out.printf("%d dollors\n",d);

                  if(q==1)
                    System.out.printf("%d quarter\n",q);
                  else if (q!=0)
                    System.out.printf("%d quarters\n",q);
                  if(di==1)
                    System.out.printf("%d dime\n",di);
                  else if (di!=0)
                    System.out.printf("%d dimes\n",di);
                  if(n==1)
                    System.out.printf("%d nickel\n",n);
                  else if (n!=0)
                    System.out.printf("%d nickels\n",n);
                  if(p==1)
                    System.out.printf("%d penny\n",p);
                  else if (p!=0)
                    System.out.printf("%d pennies\n",p);
                  

                }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
Using the code below as a template, write a program that asks the user to enter...
Using the code below as a template, write a program that asks the user to enter two integers, and finds and prints their greatest common denominator (GCD) in Java! package cp213; import java.util.Scanner; public class Lab01 { /** * @param a * @param b * @return */ public static int gcd(int a, int b) { // your code here } /** * @param args */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int a = 0;...
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
Question 1. The following code asks the user to enter the password and check it with...
Question 1. The following code asks the user to enter the password and check it with the pre-defined password that stored in the system. If the entered password is correct, it will authenticate the user to log-in into the account, otherwise reject! Analyze the following code, how it works? Do you think this code is safe? Justify? Try to overwrite this code and report the outputs? Define any vulnerabilities in this code if exist? What we can do to make...
---- Python CIMP 8A Code Lab 4 Write a program that asks the user to enter...
---- Python CIMP 8A Code Lab 4 Write a program that asks the user to enter 5 test scores. The program will display a letter grade for each test score and an average grade for the test scores entered. The program will write the student name and average test score to a text file (“studentgrades.txt”). Three functions are needed for this program. def letter_grade( test_score) Test Score Letter Grade 90-100 A 80-89 B 70-79 C 60-69 D Below 60 F...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663. This is my code, but I cannot figure out where to go from here. #set new number new_number = "" #split number split_num = phone.split("-") for char in split_num[1:2]:...
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
Write a program that does the following in order: 1. Asks the user to enter a...
Write a program that does the following in order: 1. Asks the user to enter a name 2. Asks the user to enter a number “gross income” 3. Asks the user to enter a number “state tax rate” 4. Calculates the “Federal Tax”, “FICA tax” and “State tax” 5. Calculates the “estimated tax” and round the value to 2 decimal places 6. Prints values for “name”, “gross income” and “estimated tax” The program should contain three additional variables to store...
Q1) Create a program that do the following: 1. Asks the user to enter n marks...
Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for a given mark and prints the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT