Thursday 22 March 2012

C Programming - Sample - 2 - Interest Calculation

Interest Calculation Programming

The program in Fig. calculates the value of money at the end of each year of investment, assuming an interest rate of 11 percent and prints the year, and the corresponding amount, in two columns. The output is shown in Fig.  for a period of 10 years with an initial investment of 5000.00. The pro¬gram uses the following formula: 

Value at the end of year = Value at start of year (1 + interest rate) 

In the program, the variable value represents the value of money at the end of the year while amount represents the value of money at the start of the year. The statement

amount = value 

makes the value at the end of the current year as the value at start of the next year.


Interest Calculation


 Let us consider the new features introduced in this program. The second and third lines begin with #detine instructions. A #define instruction defines value to a symbolic constant for use in the pro-gram. Whenever a symbolic name is encountered, the compiler substitutes the value associated with the name automatically. To change the value, we have to simply change the definition. In this exam-ple, we have defined two symbolic constants PERIOD and PRINCIPAL and assigned values 10 and 5000.00 respectively. These values remain constant throughout the execution of the program.

Investment Program Output


The #Define Directive:
A #define is a preprocessor compiler directive and not a statement. Therefore #define lines should not end with a semicolon. Symbolic constants are generally written in uppercase so that they are easily distinguished from lowercase variable names. #define instructions are usually placed at the beginning before the main() function. Symbolic constants are not declared in declaration section.

We must note that the defined constants are not variables. We may not change their values within the program by using an assignment statement. For example, the statement

PRINCIPAL = 10000.00;
is illegal.
The declaration section declares year as integer and amount, value and inrate as floating point numbers. Note all the floating-point variables are declared in one statement. They can also be declared as

fl oat amount ;

fl oat value; 
float inrate;


When two or more variables are declared in one statement, they are separated by a comma.
All computations and printing are accomplished in a while loop, while is a mechanism for evalu¬ating repeatedly a statement or a group of statements. In this case as long as the value of year is less than or equal to the value of PERIOD, the four statements that follow while are executed. Note that these four statements are grouped by braces. We exit the loop when year becomes greater than PERIOD. The concept and types of loops .
C supports the basic four arithmetic operators (—, +, /) along with several others.



No comments:

Post a Comment