Question

In: Computer Science

develop an algorithm and then a C program that accomplishes the following. determines the minimum number...

  • develop an algorithm and then a C program that accomplishes the following.
    • determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive;
    • produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input;
    • terminate if 0 or a negative number is entered.
    • Here is possible example of the program running (remember that there could be any number of inputs (perhaps 1,000,000) but all inputs are integers)

Hints: use if / else if / else... use % and /... use a while loop or a do while loop, no arrays

example:

Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  31
             Quarters: 1   Dimes: 0   Nickels: 1   Pennies: 1

               Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  89
               
Quarters: 3 Dimes: 1 Nickels: 0 Pennies: 4
               Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  121
   Your entered a value outside the range.

               Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  0
   Your entered a value outside the range.
               
Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  18
               Quarters: 0 Dimes: 1 Nickels: 1 Pennies: 3
   Your entered a value outside the range.

               Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  -666   
               Thanks for using my program. Good bye.

Solutions

Expert Solution

program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
        int amount; /* how much change the user has */
    int leftover; /* used to hold amount as it decreases */
    int numquarters; /* how many quarters */
    int numdimes; /* how many dimes */
    int numnickels; /* how many nickels */
    int numpennies; /* how many pennies */

    /* read in the amount and save it */
    printf("Please enter the number of cents, from 1 through 99, or enter a negative number to quit:");
    scanf("%d", &amount);
    while(amount>=0)
    {
       if(amount == 0 || amount>99)
       {
           printf("Your entered a value outside the range.\n");
       }
       else
       {
           leftover = amount;
           /* computer the number of quarters and how much is left over */
           numquarters = leftover / 25;
           leftover = leftover % 25;
           /* computer the number of dimes and how much is left over */
           numdimes = leftover / 10;
           leftover = leftover % 10;
           /* computer the number of nickels and how much is left over */
           numnickels = leftover / 5;
           leftover = leftover % 5;
           /* whatever is left over is the number of pennies */
           numpennies = leftover;
           /* print the result */
           printf("Quarters: %d Dimes: %d Nickels: %d Pennies: %d\n", numquarters, numdimes, numnickels, numpennies);
       }
       printf("Please enter the number of cents, from 1 through 99, or enter a negative number to quit:");
       scanf("%d", &amount);
   }
   printf("Thanks for using my program. Good bye.");
}

output:


Related Solutions

develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
Prime Number Determines if a number is prime or not I also need the algorithm and...
Prime Number Determines if a number is prime or not I also need the algorithm and pseudocode in java.
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5,...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing. To Do 1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not...
Develop an Algorithm and java program using Design Recipe for the following problems. Draw a flowchart...
Develop an Algorithm and java program using Design Recipe for the following problems. Draw a flowchart to compute the largest and smallest of 4 numbers : Write a Java Program for the above flowchart, use methods(functions), and nested if-else statements. Take input from the user using the Scanner object. Use separate methods to compute the Largest and Smallest of the numbers. Method 1 Name: findLargest(param 1, param 2, param 3, param 4) Method 2 Name: findSmallest(param 1, param 2, param...
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a)...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a) Create six nodes of integers such as n0, n1, n2, n3, n4, n5 (n0 is the head) b) Assign data to each nodes such as n1->data = 2; n2->data = 5, n3->data = 3, n4->data = 10, n5->data = 1. c) Make n0 ->next to point to n1, and n0->prev to point to NULL 2.) Print out the content of list you have created...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of tasks relating to student marks scored in a number of assessments including: displaying all marks, adding new student marks, calculating the mean score, median score, finding the minimum and maximum scores as well as displaying the average mark of a given student. The problem: Student marks are kept in a text file as a single column. Each student may have a different number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT