An interactive C program to calculate Compound interest with formula

By | August 3, 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.

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:

 

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

#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();
}

SCREENSHOTS:-

C_program_Compound_Interest_Formula

C_program_Compound_Interest_Formula_Output

Leave a Reply