facebook like button

29 March, 2011

program 20: Calculating compound interest

The need:
      There are many programs which I wrote just to implement the formulas I had seen in my school days. This program is one of those. But you will understand the purpose of putting this program in this post in next post.

The code:
----------------------------------------------
#include<stdio.h>
main()
{
    int p,n,m;
    float r,ci,q=1;
    printf ("For compound interest enter the values of p,n,r...\n");
    printf("p= ");
    scanf("%d",&p);      //scanning p
    printf("n= ");
    scanf("%d",&n);
      //scanning n
    printf("r=");
    scanf("%f",&r);
      //scanning r
    for (m=n;m >0;m--)   //for loop
    q=q*(1+r/100);
    ci=q*p-p;
    printf("ci= ");
    printf("%.2f\n",ci);
}

----------------------------------------------
The approach:
     This program is simple enough to understand if you know the terms and the formula used to calculate compound interest.

                ci=p((1+r/100)^n - 1);

Where the notations used are:
ci  ===> compound interest;
p  ===> primary amount;
n  ===> number of units of time;
r  ===> rate of interest in % for above unit of time;

for example:
p=10000Rs.
n=2yrs.
r=10% per year.
Then 
ci=2100Rs.
This can be crosschecked with the program.

     Now let me explain how the program is doing this. The for loop is for calculating value of (1+r/100)^n. It multiplies (1+r/100) 'n' times. Here 'm' is the counter used(may be I don't need to tell you next time) and 'q' is a float variable which has been initialized to value 1. So now you can clearly see that after loop is finished 'q' will be having value (1+r/100)^n. Rest all thing are very clear.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply