In: Computer Science
C++ language code plzzzzz
As we know, here're bills with values: 1, 5, 10, 20, 50, 100. Now, suppose you have enough amount for each kind of bill, when given a price, decide how many bills of each kind you should pay so that the total number of bills is minimized. Output the number of each bill you need to pay in the order 1, 5, 10, 20, 50, 100.
SAMPLE INPUT
77
SAMPLE OUTPUT
2 1 0 1 1 0
---> since two for 1 bill 1 for 5bill 0 for 10 bill 1for 20 bill 1 for 50bill 0 for 100bill(2*1 + 1*5 +0*10 +1*20 + 1*50 + 0*100)
main.cpp:
#include <iostream>
using namespace std;
int main(){
int amt;
// declare an array of size 6
// to store bill count for different bill value
// a[0] for bill value 1
// a[1] for bill value 5
// a[2] for bill value 10
// a[3] for bill value 20
// a[4] for bill value 50
// a[5] for bill value 100
// initialize all array value to 0
int nb[6] = {0};
cout<<"Enter total amount: ";
cin>>amt;
// pay for bill value 1
if (amt % 5 != 0){
nb[0] = amt % 5;
amt = amt - nb[0];
}
// pay for bill value 5
if (amt % 10 != 0){
nb[1] = (amt % 10) / 5;
amt = amt - nb[1] * 5;
}
// pay for bill value 10 and 20
if (amt % 50 != 0){
int temp = amt % 50;
// pay for bill value 10
if (temp % 20 != 0){
nb[2] = (temp %
20) / 10;
temp = temp -
nb[1] * 10;
}
// pay for bill value 20
nb[3] = temp / 20;
amt = amt - temp;
}
// pay for bill value 50
if (amt % 100 != 0){
nb[4] = (amt % 100) / 50;
amt = amt - nb[4] * 50;
}
// pay for bill value 100
nb[5] = amt / 100;
// print array
for (int i=0; i<6; i++){
cout<<nb[i]<<" ";
}
}
Screenshot of code:
Output:
1)
2)