www.digitalmars.com               DMDScript   C & C++
Last update Wed May 12 17:42:20 2010

>Example: Mortgage Calculator

This is a mortgage calculator, handy if you're looking to get a loan or refinance an existing one:
println("Mortgage amount:");
mortgage = readln();

println("Mortgage term in years:");
years = readln();

println("Mortgage interest rate:");
rate = readln();

// Convert to monthly interest rate
rate = Math.pow(1 + (rate / 100), 1 / 12) - 1;

months = years * 12;

// Compute total amount to be paid / mortgage
t = Math.pow(1 + rate, months);

if (t == 1)
    payment = mortgage / months;
else
    payment= (mortgage * t * rate) / (t - 1);

// Round to penny
payment = Math.round(payment * 100) / 100;

println("Monthly payment is " + payment);
It demonstrates how to use the readln and println extensions in the DMDScript console script engine. To run, save the script text above in the file mortgage.ds, and run with the command:
C:\dmdscript>ds mortgage.ds
Digital Mars DMDScript 1.03
www.digitalmars.com
Compiled by Digital Mars DMD D compiler
Copyright (c) 1999-2005 by Digital Mars
written by Walter Bright
1 source files
Mortgage amount:
100000
Mortgage term in years:
30
Mortgage interest rate:
6
Monthly payment is 589.37

C:\dmdscript>