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...
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (%...
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (% & loop) 1. Euclid’s Algorithm An alternative of the Euclidean algorithm for finding greatest common divisors (GCD) is repeatedly performing the modulo operation on two numbers until the remainder is 0. Here is the pseudocode for find the GCD of two positive numbers a and b using the Euclidean algorithm :while b ≠ 0 temp = b b = a mod t a =...
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
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
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 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT