A C program to write a function that calculates the compounded interest amount for a given initial amount, interest rate & no. of years. The interest is compounded annually. The return value will be the interest amount

By | August 27, 2013

First thing to know before writing this program is the Simple interest Formula.

COMPOUND INTEREST = (P *(1+R) N)

• P = PRINCIPAL AMOUNT.
• N = NUMBER OF YEARS (TIME).
• R = RATE OF INTEREST.

Here the problem is the rate of interest should be in the form of 0.10 instead of 10%.

Now, find out the variables needed for calculating Compound interest.

CI = (P *(1+R) N)

By looking at the formula, we have CI, P,  N, R. (i.e. Four Variables)

Now decide the data type.

int, float, and double are the choices for the variable to be used, since we are using only fraction number and not the whole number due to the complexity of the formula(i.e. rate of interest should be in the form of 0.10 instead of 10%). Hence we will omit int. Select float and double as our data type.

Two variables which will hold bigger values compared to other are SI and P. hence we will use double data type with qualifier for SI and P. For the other two N and R float data type will be sufficient.

 Source code
``````float N,R;
double CI,P;``````

data type and variables selection done.

Interactive means the program will not have any hardcoded values assigned to variables but user has to provide them.

To get values from the user screen we have a function called scanf() which will help us to scan values entered by the user by keyboard and visible on the screen.

We cannot expect that user should enter values in the sequence blindly without knowing what to enter. For that purpose we should guide the user to enter expected values by displaying message.

(E.g. ENTER THE PRINCIPAL AMOUNT: ) than scan the value entered for principal in P. and so on.

CI = (P *(1+R) N);

This formula has a raise to N this can be achieved by using a inbuilt function called pow() function which is there in another header file called math.h So we have to INCLUDE math.h in our program.

e.g.  XY can be written as pow(X,Y) and So.

This COMPOUND interest formula cannot be written on a single line statement. So we write it in two statements as:

 Source code
```CI=pow(1+R,N);

CI=P*CI;

printf("THE CALCULATED SIMPLE INTEREST IS RUPEES : %.2f",CI);```

Here we are using %.2f instead of %f so that the Amount is Printed to Two Decimal Digits.

Note:- Remember whenever you are scanning or printing the values of double be careful to  write %lf instead of just %f. Because %lf is used for double data type and %f is used for float data type.

C program code

 Source code
``````#include<stdio.h>
#include<math.h>
void main()
{
float N,R;
double CI=0,P;
clrscr();
printf("ENTER THE PRINCIPAL AMOUNT : ");
scanf("%f",&P);
printf("ENTER THE NUMBER OF YEAR(S) : ");
scanf("%f",&N);
printf("ENTER THE RATE OF INTEREST(%) : ");
scanf("%f",&R);
R=R/100;
CI=pow(1+R,N);
CI=P*CI;
printf("THE CALCULATED SIMPLE INTEREST IS RUPEES : %.2f",CI);
getch();
}``````

Use the following function definition: float comp_int_calc(floatint_amt, float rate, int years); Write a program that will accept the initial amount, interest rate & the no. of years and call the function with these values to find out the interest amount and display the returned value

Now the thing we have to do is split the above program in to two parts

One part will be the MAIN function part and the other part will be the COMP_INT_CALC function part

Here, since the definition of function is already given (i.e.float comp_int_calc(floatint_amt, float rate, int years) we need to change the variables from the old data types to the new defined data types in the function. And also declare the prototype of the function used there itself. (Note: declaration of the function can be done with variable name or only with data type excluding varaible names)

 Source code
``````float R,P,CI;
int N;
float comp_int_calc(float,float,int);``````

Now, in main function only the calculation code will go to the function body part and else every thing will be the same.

 Source code
``````void main()
{
float R,P,CI;
int N;
float comp_int_calc(float,float,int);
clrscr();
printf("ENTER THE PRINCIPAL AMOUNT : ");
scanf("%f",&P);
printf("ENTER THE NUMBER OF YEAR(S) : ");
scanf("%d",&N);
printf("ENTER THE RATE OF INTEREST(%) : ");
scanf("%f",&R);
R=R/100;
CI=comp_int_calc(P,R,N);
printf("THE CALCULATED SIMPLE INTEREST IS RUPEES : %.2f",CI);
getch();
}``````

Note:- Becareful while passing the parameters or arguments to the function keep the sequence same or there will be error in the program.

float comp_int_calc(float AMT,float RATE,int YEARS)

CI=comp_int_calc(P,R,N);

In the function, we can keep the same name of the varaibles we are passing or use the new names altogether. The scope of main function variables are limited to main function itself. The variables with the same name in the outer function will have scope inside the function only.

After changing the names to variable according to the defination of the function, we have function to be as below:

 Source code
``````float comp_int_calc(float AMT,float RATE,int YEARS)
{
float COMP_INT=0;
COMP_INT=pow(1+RATE,YEARS);
COMP_INT=AMT*COMP_INT;
return COMP_INT;
}``````

The complete program will be as below:

C program code

 Source code
`#include<stdio.h>#include<math.h>void main(){float R,P,CI;int N;float comp_int_calc(float,float,int);clrscr();printf("ENTER THE PRINCIPAL AMOUNT : ");scanf("%f",&P);printf("ENTER THE NUMBER OF YEAR(S) : ");scanf("%d",&N);printf("ENTER THE RATE OF INTEREST(%) : ");scanf("%f",&R);R=R/100;CI=comp_int_calc(P,R,N);printf("THE CALCULATED SIMPLE INTEREST IS RUPEES : %.2f",CI);getch();} float comp_int_calc(float AMT,float RATE,int YEARS){    float COMP_INT=0;    COMP_INT=pow(1+RATE,YEARS);    COMP_INT=AMT*COMP_INT;    return COMP_INT;}`

SCREEN SHOTS:-

Break up the program that you wrote to solve above problem into two separate source files. The main function should be in one file & the calculation function must be in another file. And modify the program so that the interest rate is a symbolic constant and is no longer input from the keyboard. And put all the C preprocessor directives into a separate header file that is included in the two program source files.

#include is an instruction used to read the functions written in header files which are also called library files.

#include can be used with < > or ” “  e.g. #include<compound.h> or #include”compound.h” both are used depending upon the file located. If the included file is located in the library files or the same location where the other header files than we have to use <>   e.g. #include<compound.h> and If the included file is located in the same folder where the calling program is present than we have to use ” ”   e.g. #include”compound.h”.

Now, cut the function part and save it in another name the file compound.h

File :- compound.h

 Source code
``````float comp_int_calc(float AMT,float RATE,int YEARS)
{
float COMP_INT=0;
COMP_INT=pow(1+RATE,YEARS);
COMP_INT=AMT*COMP_INT;
return COMP_INT;
}``````

Now, the changes needed are only including a file by #include”compound.h” and there is no need of declaration of function, so delete that function declaration or comment that line by just adding \\ infront of the line which is to be commented. If you need to comment several lines then you can use /*on the first line and */ on the last line to be commented.

Note:- comment has no effect inside the code. It’s basically used for for giving  information to the user thought comments.

C program code

 Source code
`#include<stdio.h>#include<math.h>#include"compound.h"void main(){float R,P,CI;int N;//float comp_int_calc(float,float,int);clrscr();printf("ENTER THE PRINCIPAL AMOUNT : ");scanf("%f",&P);printf("ENTER THE NUMBER OF YEAR(S) : ");scanf("%d",&N);printf("ENTER THE RATE OF INTEREST(%) : ");scanf("%f",&R);R=R/100;CI=comp_int_calc(P,R,N);printf("THE CALCULATED SIMPLE INTEREST IS RUPEES : %.2f",CI);getch();}`

SCREEN SHOTS:-