I want to write a code that divides a number into several different intervals and performs special calculations for each interval as follows. At the end, show the sum of all the obtained numbers. I tried several methods and it did not work.
Calculation formula:
| Bracket | Rate |
|------------|------|
| 0-500M | 8% |
| >500M-2B | 7% |
| >2B-10B | 5% |
| >10B-30B | 4% |
| >30B | 3% |
For example, for the number 3,500,000,000, the answer should be 220,000,000.
| Bracket | Amount | Rate | Total |
|------------|----------|------|-------|
| 0-500M | 500M | 8% | 40M |
| >500M-2B | 1,500M | 7% | 105M |
| >2B-10B | 1,500M | 5% | 75M |
| Total | 3,500M | - | 220M |
The code I wrote:
function calculate(inputnumber: Int64): Extended;
var
a,b,c,d : Extended;
begin
d := (INPUTNUMBER - 500000000) ;
c := (d - 2000000000 ) * (INPUTNUMBER * 0.07);
b := (c - 10000000000) * (INPUTNUMBER * 0.05);
a := (b - 30000000000) * (INPUTNUMBER * 0.04);
Result := a+b+c+(d * 0.08);
end
I want to write a code that divides a number into several different intervals and performs special calculations for each interval as follows. At the end, show the sum of all the obtained numbers.
When you turn to SO with homework question, it is fair to indicate that your question concerns homework. Please do so in the future.
Your task is to write code for each of those intervals and if you do it in a ladder fashion from highest capital value down to zero, then the entry point to the ladder depends on the capital value.
Each step would look as follows:
// step 5
if capital > 30000000000 then // > 30 billion
begin
interest := interest + 0.03 * (capital-30000000000);
capital := 30000000000; // remaining (unaccounted) capital falling through
end;
The following step would have the condition:
// step 4
if capital > 10000000000 then // > 10 billion
begin
// interest calculation with applicable interest rate
// remaining capital (10 billion) to fall through again to following step
end;
Finally, after a few more steps, the last one:
// step 1
if capital > 0 then
begin
interest := interest + 0.08 * (capital);
end;
This gives the result you are expecting.