facebook like button

29 March, 2011

program 21: Compound interest (introduction of math.h)

The need:
     This is the same program of previous post. But in previous post I said the purpose of that program you would come to know in next(i mean this post.) So the need of this program is to introduce you one more header file "math.h".

The code:
----------------------------------------------
/*Note that in this program I have
used two header files*/
#include<stdio.h>
#include<math.h>
main()
{
    int p,n;
    float r,ci,q,s;
    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
    s=(1+r/100);     //assigning value of (1+r/100) to s
    q=pow(s,n);      //pow function calculating (s^n)
    ci=q*p-p;
    printf("ci= ");
    printf("%.2f\n",ci);
}
----------------------------------------------

The approach:
     In previous program you have already been told everything about compound interest. There we calculated the value of (1+r/100)^n by multiplying (1+r/100), n times. Here with math.h we directly calculated the value of (1+r/100)^n by using built-in function pow().

Remarks:
   1.  pow() is not only function in "math.h". There are other functions also. In our program we'll be using those whenever we need. Those will be given in next post.
   2. If you are using linux/unix to compile this program, add '-lm'(without quotes) after the compile command. Ex. : if the file name is file.c, give the command: 
"cc file.c -lm"(without quotes)
instead of only "cc file.c"

No comments:

Post a Comment

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