Sample program 3: Interest calculation in C

2 minute read
0

Interest Calculation

In this session, we'll calculate the value of money at the end of each year of investment of 5000.00 for the period of 10 years. Assuming an interest rate of 11 percent and prints the year and the corresponding amount. 
The formula we'll use :
        Value at the end of year = value of start year(1+interest rate)
 


The program:


1    /*...............Investment Problem................*/
2    #define PERIOD 10
3    #define PRINCIPAL 5000.00
4    /*..... the main program begins........*/
5    main()
6    {
7        int year;
8        float amount, value, inrate;
9    /*.......... ASSIGNMENT STATEMENT..........*/
10      amount= PRINCIPAL;
11      inrate= 0.11;
12      year= 0;
13  /*.....  COMPUTATION USING while LOOP........*/
14       while(year <= PERIOD)
15       {
16            printf("%d     %8.2f\n", year, amount);
17            value= amount + inrate * amount;
18            year= year + 1;
19            amount = value;
20        }
21   /*.......... while LOOP ENDS .......*/
22   }

The #define directive

The second and third lines begin with #define directive. It is a preprocessor compiler directive and not a statement. Therefore #define lines should not end with a semicolon(;). It describes the value as a symbolic constant for use in the program. Symbolic constant means the assigned value will be fixed throughout the entire program.

Symbolic constants are generally written in uppercase so that they are easily distinguished from lowercase variable names. #define directives are usually placed at the beginning before the main() function. This preprocessor directive will be discussed later.

Here in this program, we have defined two symbolic constants PERIOD and PRINCIPAL assigned values 10 and 5000.00 respectively. These values remain constant throughout the execution of the program.

*Note, The defined constants are not variable. We can't change their values within the program by using an assignment statement. Example:
    
        PRINCIPAL= 10000.00;

is wrong.

In line 14 there is a function called while() is a mechanism for evaluating repeatedly a statement or a group of statements. It is a loop and it will continue executing the statement given to it unless and until the condition given to the while loop is false. Then the loop will stop.


----------------------------------------------------------------------------------------------------------------------------


Explore our website for valuable C programming resources and enhance your coding skills today!.


Learn C programming language from basic to advanced here (Click here or on the image)


Stay Strong, keep learning, and keep growing.




Post a Comment

0Comments
Post a Comment (0)