* Philippine currency is broken down into 1000,500,100,50,20,10 peso bills and 1 peso coin.
Write a program that would input an integer amount in peso and output the breakdown in bills and coins that would add up to that amount. For example, the amount 3,998 would be, 3 P100-bills,1 P500-bills, 4
P100-bills,1 P50-bill,1 P20-bill, 1 P10-bill,8 P1-coin.
* (Summing the digits in an integer) write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. for example, if an integer is 932, the sum of all its digits is 14.
i tried to do this and as usual it didnt work..
ahmM.. i'm looking for some1one who will do this, with explanation... f its ok,,
so i'll learn.. tnx..
I've done a few of your programs for you and I can tell you that by solving your problem here for you, you will not learn anything. Don't go over your head with maths in your programs, it will slow you down. First learn the concepts that C++ uses, then start building programs. Letting us solve your problem is generally not useful for you.
I'm prepared to give you the efficient code for both of these problems, but don't expect them to be either easy to learn from or expect from me to create any program for you afterwards. Tell me whether you want it or you don't want it.
A small example on remainders:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
int main()
{
std::cout << "5/3=" << 5/3 << std::endl; // Integer division
std::cout << "5%3=" << 5%3 << std::endl; // Modulo, aka, remainder
std::cout << "(5/3)*3=" << (5/3)*3 << std::endl;
// Due to the fact that integer division discards the decimal part of the expression,
// multiplying the outcome of an integer division by the dividend, would give you the
// amount of 3's (in this case) are in the starting integer. In this case 3 can be fully
// placed inside 5 exactly one time and thus the outcome is 1*3=3 (instead of 5).
system("pause"); // Calls some BATCH codes to keep the window open
return 0;
}
Heh, it's just that I am willing to invest time in helping people, but mainly that is not through giving the concrete answer to the question, rather guiding someone to the final answer. It's up to you to choose. Just a side-note, click the icon with the piece of paper and the pencil to edit your posts, some people are not really fond of double-posters. ;)
The second uses integer division as well, although the amounts to divide by might be a little less obvious. I will show you how I did the first one, in contradiction to yours. You might actually understand. :P (I'll still help you after, since this ain't spoiling any results, just showing a different approach). Here goes:
#include <iostream>
int main()
{
unsignedint Money(0);
unsignedint Thousand(0);
unsignedint FiveHundred(0);
unsignedint Hundred(0);
unsignedint Fifty(0);
unsignedint Twenty(0);
unsignedint Ten(0);
std::cout << "Insert the amount of cash to check: ";
std::cin >> Money;
std::cin.clear(); // When wrong input is given (digits mixed up with characters like 'y'), your program won't crash, when none-wrong input is given, the right value is in Money
Money-=(Thousand = Money/1000)*1000;
Money-=(FiveHundred = Money/500)*500;
Money-=(Hundred = Money/100)*100;
Money-=(Fifty = Money/50)*50;
Money-=(Twenty = Money/20)*20;
Money-=(Ten = Money/10)*10;
std::cout << "1000 bills: " << Thousand << std::endl;
std::cout << "500 bills: " << FiveHundred << std::endl;
std::cout << "100 bills: " << Hundred << std::endl;
std::cout << "50 bills: " << Fifty << std::endl;
std::cout << "20 bills: " << Twenty << std::endl;
std::cout << "10 bills: " << Ten << std::endl;
std::cout << "Spare coins: " << Money << std::endl;
system("pause"); // Use Batch to keep the console open
return 0;
}
As shown, I use Money to keep track of the amount left (instead of doing nested modulo). This also means that I dont have to do the last equation, since Money is already in a good state at the end (since it is always a value between 10 and 0).