
GREETINGS!

This file contains information on the Savings calculator program provided.
The program and source code are provided free of charge by Robert J. Manning,
South Bay Computer Assistance. You may distribute the program and source 
code as you wish, provided all files listed are included in the distribution:

	SAVINGS.BAS    BASIC language source code file
	SAVINGS.C      C language source code file
	SAVINGS.EXE    Executable DOS program (C version)
	SAVINGS.PAS    Pascal language source code file
	SAVINGS.TXT    Information file

All files are freeware, hereby donated to the public domain. I hope you find
these programs useful and informative!


CONTACT ADDRESSES:

Feel free to drop me an email any time, I check my mail often.
The author may be contacted at the following email addresses:
	
	robertm782@aol.com
	76022.1630@compuserve.com

For information on other programs, visit the SBCA Shareware Center 
web page at:

	http://members.aol.com/robertm782/private/sbcapage.htm


WHAT THE PROGRAM DOES

This program is meant to demonstrate the algorithm used for calculating
yield on an investment account. It can come in handy when trying to find
out what your return on an investment over time will be. Let's say you
want to know the difference between placing $1000 in a normal savings
account at 5% interest compounded quarterly, and putting the same amount
in a checking account at 3% interest compounded daily. This program allows
you to find out how much you'll earn in interest over a given period, so
you can compare the returns, and make the best investment choice.

C VERSION

The source code should compile with any standard C compiler. The program
enclosed is the C version shown below. For completeness, after the C
code is shown the same basic algorithm in both BASIC and Pascal.

The coding in the C version has been doctored a bit for appearance, and
contains some error traps. The other versions do not contain much in the
way of error traps, and they're not particularly fancy. Their appearance
is left to you to code in.

BASIC VERSION

The file SAVINGS.BAS will operate in either MS-DOS QBasic or QuickBasic.
Again, it's a bare-bones version, without error traps or a nice interface.
Adding such features to the code is left to you.

Pascal VERSION

The file SAVINGS.PAS should operate under a Borland Pascal DOS compiler.
It can be changed to operate in TPW by changing the 'USES' line to:

	uses dos, wincrt;

It is also a bare-bones version, without error checking or a nice interface.

========================================================================
//Source code for the program enclosed. Comments are shown here and not
//in the source code file.

// include declarations:

#include <conio.h>
#include <stdio.h>
#include <math.h>

// function to abort program and explain why:
void end(int reason);

// main body of program:
void main(void)
{
	float amount = 0.0, interest = 0.0, endBalance = 0.0, years = 0.0;
	int comp = 0, choice = 0;

	do
	{
		// add some color to the screen and a title display:
		textcolor(15);
		textbackground(1);
		clrscr();
		gotoxy(12,3);
		cprintf("ͻ\r\n");
		gotoxy(12,4);
		cprintf(" Calculator to find savings account balance over time. \r\n");
		gotoxy(12,5);
		cprintf("   Utility program courtesy of Robert Manning, SBCA.   \r\n");
		gotoxy(12,6);
		cprintf("ͼ\r\n");

		//inform the user of the calculators' function:
		gotoxy(20,8);
		cprintf("The formula used for this calculation is:");
		gotoxy(4,9);
		cprintf("Amount = Principle * ((1 + Interest/Compounding) ^ (Compounding * Years))");

		//get starting balance of account, exit if incorrect entry:
		gotoxy(13,11);
		cprintf("Enter the current account balance: $");
		cscanf("%10f", &amount);
		if (amount <= 0.0) end(1);

		//get interest rate, exit if entry <= 0. Note that we're assuming
		//that the user will enter the number in percent, not decimal. So,
		//we'll divide by 100 if the entry exceeds 1 (indicating percent).
		//If the user happens to enter a decimal value, do nothing.
		gotoxy(13,12);
		cprintf("Enter the interest rate (ie, enter 5 for 5%): ");
		cscanf("%10f", &interest);
		if (interest <= 0.0) end(2);
		if (interest > 1) interest /= 100;

		//get the number of compounding periods per year. proper values
		//for this entry are 1 to 365, though no upper limit is checked.
		gotoxy(13,13);
		cprintf("Enter compounding periods per year (ie, 12 for monthly): ");
		cscanf("%5d", &comp);
		if (comp < 1) end(3);
		
		//get the number of years to use for calculation. Note that this
		//version of the program will accept a decimal value, in case the
		//user wants to figure the amount over something less than a year,
		//or fractions of years.
		gotoxy(13,14);
		cprintf("Enter the time in years to consider this investment: ");
		cscanf("%5f", &years);
		if (years < 0) end(4);

		//now take all this information and compute the value in endBalance:

		endBalance = pow(((interest/comp)+1), (comp * years));
		endBalance *= amount;
		
		//display the information in the form of the formula shown at the
		//start of the program:
		gotoxy(13,16);
		cprintf("%1.2f = %1.2f * ((1 + %1.2f / %d) ^ (%d * %1.1f))",
			endBalance, interest, interest, comp, comp, years);
		gotoxy(13,18);

		//explain what happened:
		cprintf("So, the balance of your $%1.2f account at the end of", amount);
		gotoxy(13,19);
		cprintf("%1.1f years, at %1.2f%% interest will be $%1.2f", years, interest, endBalance);
		gotoxy(13,21);
		
		//ask for another calculation or exit:
		cprintf("Calculate again (Y/N) ");
		while (kbhit()) getch();
		choice = getch();
	} while ((choice == 'Y') || (choice == 'y'));

//clean up and quit:
textcolor(7);
textbackground(0);
return;
}

//this function lets the user know what there improper entry was and
//exits the program:

void end(int reason)
{
	textcolor(7);
	textbackground(0);
	clrscr();
	switch (reason)
	{
		case 1: printf("Amount must be greater than zero!\n"); break;
		case 2: printf("Interest must be greater than zero!\n"); break;
		case 3: printf("Compounding must be at least 1 (Annual)!\n"); break;
		case 4: printf("Time in years must be greater than zero!\n"); break;
	}
	printf("Sorry, program terminated!\n");
	abort();
	return;
}

========================================================================
The program code for a BASIC version is shown next. This code will
work in QBasic or QuickBasic:

DO
	 CLS
	 amount! = 0
	 interest! = 0
	 comp% = 0
	 years% = 0
	 endBalance! = 0
	 PRINT "Calculator to find savings account balance over time."
	 PRINT
	 INPUT "Enter the current account balance: "; amount!
	 INPUT "Enter the interest rate (ie, enter 5 for 5%): "; interest!
	 IF interest! > 1 THEN interest! = interest! / 100
	 INPUT "Enter compounding periods per year (ie, 12 for monthly): "; comp%
	 INPUT "Enter the time in years to consider this investment: "; years%
	 PRINT
	 PRINT "The formula used for this calculation is:"
	 PRINT "Amount = Principle * ((1 + Interest/Compounding) ^ (Compounding * Years))"
	 PRINT
	 PRINT "Using the amounts you entered, this formula becomes:"
	 endBalance! = (((interest! / comp%) + 1) ^ (comp% * years%)) * amount!
	 PRINT endBalance!; "="; amount!; "* ((1 +"; interest!; "/"; comp%; ") ^ ("; comp%; "*"; years%; "))"
	 PRINT
	 PRINT "So, the balance of your $"; amount!; "account at the end of"; years%; "years,"
	 PRINT "at"; interest! * 100; "% interest will be $"; endBalance!
	 PRINT
	 INPUT "Calculate again (Y/N) "; choice$
LOOP WHILE UCASE$(choice$) = "Y"
END

========================================================================
Lastly, the program code for a Pascal version:

program savings(input, output);
uses dos, crt;
var
	amount, interest, endBalance: real;
	comp, years: integer;
	choice: string;

{ Note Pascal doesn't have a power function, so here's one to use: }

function Power(Y, X: real): real;
	begin
		Power := Exp(X * Ln(Y)); { Note: A^x = e^(x Ln A) }
	end;

begin {* main *}
  choice := 'Y';
  while (choice = 'Y') or (choice = 'y') do
  begin
	clrscr;
	writeln('Calculator to find savings account balance over time.');
	writeln(' ');
	writeln('Enter the current account balance: ');
	readln(amount);
	writeln('Enter the interest rate (ie, enter 5 for 5%): ');
	readln(interest);
	if (interest > 1) then interest := interest / 100;
	writeln('Enter compounding periods per year (ie, 12 for monthly): ');
	readln(comp);
	writeln('Enter the time in years to consider this investment: ');
	readln(years);
	writeln(' ');
	writeln('The formula used for this calculation is:');
	writeln('Amount = Principle * ((1 + Interest/Compounding) ^ (Compounding * Years))');
	writeln(' ');
	writeln('Using the amounts you entered, this formula becomes:');
	endBalance := Power(((interest / comp) + 1), (comp * years)) * amount;
	writeln(endBalance:2:2, ' = ', amount:2:2, ' * ((1 + ', interest:1:2, ' / ', comp:2, ') ^ (', comp:2, ' * ', years:2, '))');
	writeln(' ');
	writeln('So, the balance of your $', amount:2:2, ' account at the end of ', years, ' years,');
	writeln('at ', interest * 100:2:1, '% interest will be $', endBalance:2:2);
	writeln(' ');
	writeln('Calculate again (Y/N) ?');
	readln(choice);
  end;
end.

========================================================================

	Have fun with the program!
	
	Robert Manning
	robertm782@aol.com
	76022.1630@compuserve.com
	http://members.aol.com/robertm782/private/sbcapage.htm



