Lets say we need to write an algorithm for giving money to the bank. We need to know interest rate, money which we put every year on our account, and amount of money we want to get. F.e int. rate is 1%, money we put every year is 1000 (or whatever) and we want to get 10000 (not more). It'd look like this:
MoneyOnAccreaches 10000 (we want to print amount what was before
10000 - 9462.21)
and so on. Here is my code in Pascal.
program whilel; var interestRate:real; moneyEveryYear:real; accMoneyOut:real; totalAmount:real; accMoneyControl:real; begin readln(moneyEveryYear); readln(interestRate); readln(totalAmount); while accMoneyOut<totalAmount do begin accMoneyControl:=(accMoneyOut+moneyEveryYear)*((interestRate/100)+1); if accMoneyControl>totalAmount then break; accMoneyOut:=(accMoneyOut+moneyEveryYear)*((interestRate/100)+1); end; writeln(accMoneyOut:6:2); readln(); end.
How can I improve this algorithm? Maybe some better logic will help.