94.6k views
3 votes
Write an algorithm and a C++ program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in 5-cent increments (25, 30, 35, … , 90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. The program asks the user to enter the price of the item. Then, the program calculates and outputs the change as illustrated in the sample outputs bellow.

1 Answer

4 votes

Answer:

// program that implement the algorithm to find the change.

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

// variables

int cost,q,d,n,p;

cout<<"Enter the cost of item between 25 cents and a dollar, in 5 increments (25, 30, 35, … , 90, 95, or 100):";

// read the cost

cin>>cost;

// find the change

int change=100-cost;

// find quarter

q=change/25;

change=change%25;

// find the dimes

d=change/10;

change=change%10;

// find the nickels

n=change/5;

// find the pennies

p=change%5;

// print the change

cout<<"Your change is: "<<q<<" quarter,"<<d<<" dimes,"<<n<<" nickels and "<<p <<" pennies."<<endl;

return 0;

}

Step-by-step explanation:

Read the cost of item from user and assign it to variable "cost".Then find the change to be returned to user.Then find the quarter by dividing the change with 25.Update the change and then find the dimes by dividing change with 10.Then update the change and find nickels and then find the pennies.Print the change to be returned.

Output:

Enter the cost of item between 25 cents and a dollar, in 5 increments (25, 30, 35, … , 90, 95, or 100):45

Your change is: 2 quarter,0 dimes,1 nickels and 0 pennies.

User Ramon Vasconcelos
by
5.4k points