In this program, we will be finding possible denomination of money input from user. In this case we are assuming denomination as 1,5,10,25. For example if input money is 65 then denomination will be 25*2,10*1,5*1. Since we are trying to get best possible denomination such that we have least coins, so will store the denomination in decreasing order. Below is the implementation:
#include <iostream>
using namespace std;
int main()
{
int denom[] = { 25,10,5,1};
int money;
cout << "Enter the money:";
cin >> money;
int i = 0,coin;
int denomlen = sizeof(denom)/sizeof(int);
if (money <= 0)
{
cout << "Money not valid";
return 0;
}
while (money > 0 && i < denomlen)
{
if (money > denom[i])
{
coin = money/denom[i];
money = money - coin*denom[i];
cout << "\n Number of coins of " <<denom[i] <<" :" << coin;
}
i++;
}
return 0;
}
If you have any feedback please comment below.