Mzsmunna
10/16/2019 - 2:00 PM

## greedy algo - return money back

``````
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
srand(time(0));
int i,j,k=0,l=0,muw,mtp,n1=0,r,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0,n9=0;
i=rand();
cout<<"Price of A Product is : "<<i<<endl;
cout<<"\nHow much money you want to pay in Taka?"<<endl;
cout<<"Enter the amount you want to pay :"<<endl;
cin>>muw;

do
{
if(muw<i)
{
cin>>mtp;
muw=muw+mtp;
}

}while(muw<i);

r=muw-i;
j=r;
do
{
if(j>=1000)
{
//1000tk
k=k+1000;
j=j-1000;
l=l+1;
n9=n9+1;
}
else if((j>=500)&&(j<1000))
{
//500tk
k=k+500;
j=j-500;
l=l+1;
n1=n1+1;
}
else if((j>=100)&&(j<500))
{
//100tk
k=k+100;
j=j-100;
l=l+1;
n2=n2+1;
}
else if((j>=50)&&(j<100))
{
//50tk
k=k+50;
j=j-50;
l=l+1;
n3=n3+1;
}
else if((j>=20)&&(j<50))
{
//20tk
k=k+20;
j=j-20;
l=l+1;
n4=n4+1;
}
else if((j>=10)&&(j<20))
{
//10tk
k=k+10;
j=j-10;
l=l+1;
n5=n5+1;
}
else if((j>=5)&&(j<10))
{
//5tk
k=k+5;
j=j-5;
l=l+1;
n6=n6+1;
}
else if((j>=2)&&(j<5))
{
//2tk
k=k+2;
j=j-2;
l=l+1;
n7=n7+1;
}
else if((j<=1)&&(j<2)&&(j!=0))
{
//1tk
k=k+1;
j=j-1;
l=l+1;
n8=n8+1;
}
}while(k<r);
cout<<"\nReturn back amount is "<<r<<" TK"<<endl;
cout<<"\nTotal Notes : "<<l<<endl;

if(n8!=0)
{
cout<<"Total Notes of 1tk : "<<n8<<endl;
}
if(n7!=0)
{
cout<<"Total Notes of 2tk : "<<n7<<endl;
}
if(n6!=0)
{
cout<<"Total Notes of 5tk : "<<n6<<endl;
}
if(n5!=0)
{
cout<<"Total Notes of 10tk : "<<n5<<endl;
}
if(n4!=0)
{
cout<<"Total Notes of 20tk : "<<n4<<endl;
}
if(n3!=0)
{
cout<<"Total Notes of 50tk : "<<n3<<endl;
}
if(n2!=0)
{
cout<<"Total Notes of 100tk : "<<n2<<endl;
}
if(n1!=0)
{
cout<<"Total Notes of 500tk : "<<n1<<endl;
}
if(n9!=0)
{
cout<<"Total Notes of 1000tk : "<<n9<<endl;
}

}
``````