Question: Write a C++ program that inputs the amount of money, calculate how many toy eggs you can buy and calculate the remaining coupons.
One egg toy = 1 Dirham
One egg toy = 1 coupon.
Five coupons = 1 Free egg toy.
For example, if you have 12 Dirhams you get 12 egg toys and 12 coupons, using the 12 coupons you can get 2 more egg toys so total will be 14 egg toys
and 2 remaining coupons.
Algorithm:
1. Prompt the user to enter the amount of money.
2. Calculate how many egg toys you can buy with money entered.
3. Calculate the remaining coupons.
4. Print the amount of egg toys you will get and the remaining coupons.
#include<iostream>
using namespace std;
int main()
{
int dirhams, toyeggs, coupons, remainingcoupons;
cout<<" Enter the amount of money: "
cin>>dirhams;
//i know i should use if else statements but i dont know how to write the formulas
cout<<" You got " <<toyeggs<< " and "<<remainingcoupons<<" remaining coupons"
return 0;
}
off the top of my head, there is probably a basic way to do it with logs without brute force, but if you just divide by 5 in a loop until you get a number less than 1, add that up, and you will get the answer with brute force.
If so then once you have the int dirhams then make
dirhams = eggs
eggs = coupons
coupons divided by 5 = eggsC
(this will give you the remainder or you can manipulate it to get a float that youll have to declare.
eggsC + eggs = outputEggs
cout<<" You got " <<outputEggs << " and "<< yourdeclaredvariable<<" remaining coupons"
return 0;
That's close. Just remember that assignment goes from right to left, so
1 2
dirhams = eggs;
eggs = coupons;
should be
1 2
eggs = dirhams;
coupons = eggs;
Also you're using remainingcoupons to represent two things: the eggs from coupons and the number of coupons that remain when you're done. I'd use two variables to make it clear:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
usingnamespace std;
int
main()
{
int dirhams, coupons, remainingcoupons;
int eggsFromDirhams, eggsFromCoupons;
cout << " Enter the amount of money: ";
cin >> dirhams;
eggsFromDirhams = dirhams;
coupons = eggsFromDirhams;
eggsFromCoupons = coupons / 5;
remainingcoupons = coupons % 5;
cout << " You got " << eggsFromCoupons + eggsFromDirhams
<< " eggs and " << remainingcoupons << " remaining coupons\n";
return 0;
}