Answer:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
srand(time(0));
int coins = rand()%99 + 1;
int temp = coins;
cout<<"Enter the coins needed to make $0."<<coins<<"?"<<endl<<endl;
int a, b, c, d;
cout<<"Quarters? ";
cin>>a;
cout<<"Dimes? ";
cin>>b;
cout<<"Nickles? ";
cin>>c;
cout<<"Pennies? ";
cin>>d;
int quarters = coins/25;
coins = coins%25;
int dimes = coins/10;
coins = coins%10;
int nickles = coins/5;
coins = coins%5;
int pennies = coins/1;
if(((a*25) + (b*10) + (c*5) + (d*1))==temp)
{
cout<<"That's correct"<<endl;
if(a!=quarters || b!=dimes || c!=nickles || d!=pennies)
{
cout<<"The optimized solution is "<<endl;
}
}
else
{
cout<<"Not correct"<<endl;
cout<<"The solution is "<<endl;
}
cout<<"\tQuarters: "<<quarters<<endl;
cout<<"\tDimes: "<<dimes<<endl;
cout<<"\tNickles: "<<nickles<<endl;
cout<<"\tPennies: "<<pennies<<endl;
return 0;
}#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
srand(time(0));
int coins = rand()%99 + 1;
int temp = coins;
cout<<"Enter the coins needed to make $0."<<coins<<"?"<<endl<<endl;
int a, b, c, d;
cout<<"Quarters? ";
cin>>a;
cout<<"Dimes? ";
cin>>b;
cout<<"Nickles? ";
cin>>c;
cout<<"Pennies? ";
cin>>d;
int quarters = coins/25;
coins = coins%25;
int dimes = coins/10;
coins = coins%10;
int nickles = coins/5;
coins = coins%5;
int pennies = coins/1;
if(((a*25) + (b*10) + (c*5) + (d*1))==temp)
{
cout<<"That's correct"<<endl;
if(a!=quarters || b!=dimes || c!=nickles || d!=pennies)
{
cout<<"The optimized solution is "<<endl;
}
}
else
{
cout<<"Not correct"<<endl;
cout<<"The solution is "<<endl;
}
cout<<"\tQuarters: "<<quarters<<endl;
cout<<"\tDimes: "<<dimes<<endl;
cout<<"\tNickles: "<<nickles<<endl;
cout<<"\tPennies: "<<pennies<<endl;
return 0;
}
Explanation:
- Use the built-in rand function to generate a random coin.
- Get all the required values for quarters, dimes, nickles and pennies and store them in variables.
- Check if the coins give a value of temp, then answer is correct and the values of user will be added.
- Optimum solution is displayed If it does not matches with the optimum solution. Display that the answer is wrong along with solution.