Search code examples
pascal

write a code that divides a number into several different intervals and performs special calculations for each interval


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:

  1. If the number is less than five hundred million, it calculates 8% of the entered number.
  2. In relation to the excess of five hundred million to two billion, calculate 7% of the entered number and add it to the previous result.
  3. In relation to the excess of two billion to ten billion, calculate 5% of the entered number and add it to the previous result.
  4. In relation to the excess of ten billion to thirty billion, calculate 4% of the entered number and add it to the previous result.
  5. From thirty billion and above, calculate 3% of the entered number and add it to the previous result.
| 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

Solution

  • 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.