An interactive C program to calculate Compound interest knowing only Simple interest

By | August 3, 2013

First thing to know before writing this program is the Simple interest Formula. SIMPLE INTEREST = (P * N * R)/100

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

Now, find out the variables needed for calculating Simple interest. SI = (P * N * R)/100 By looking at the formula, we have SI, P, N, R. (i.e. Four Variables)

Now the program for Simple interest has to be changed according to the NUMBER OF YEARS (TIME) (i.e. N variable). So we have to calculate the interest per year and pass the new PRINCIPAL AMOUNT (i.e. P variable) to the formula according to the value of NUMBER OF YEARS. So it means if N=5 the interest amount will be calculated 5 times. To do so we have something called as LOOP. LOOP is used to run the given code several times depending upon the given CONDITION to be true. There are several types of loops which are FOR loop, WHILE loop, DO-WHILE loop etc.

The loop which will be suitable for us is FOR loop. program code:-

#include<stdio.h>
void main()
{
int N,R;
long int SI=0,P;
clrscr();
printf("ENTER THE PRINCIPAL AMOUNT : ");
scanf("%ld",&P);
printf("ENTER THE NUMBER OF YEAR(S) : ");
scanf("%d",&N);
printf("ENTER THE RATE OF INTEREST(%) : ");
scanf("%d",&R);
SI=P*R*N; SI=SI/100;
printf("THE CALCULATED SIMPLE INTEREST IS RUPEES : %ld",SI);
getch();
}

So the changes in the code will be as below. Here we need two more variables ONE is i and CI. i is used for FOR loop and CI is used for COMPOUND INTEREST. SI and CI are initialized to zero so that garbage value (fake value) which is their inside variable is cleared and set to zero value. Now you will have doubt that why only SI, Because SI is not scanned for input from User and It has to be calculated.

program code:-

#include<stdio.h>
void main()
{
int i,N,R;
long int CI=0,SI=0,P;
clrscr();
printf("ENTER THE PRINCIPAL AMOUNT : ");
scanf("%ld",&P);
printf("ENTER THE NUMBER OF YEAR(S) : ");
scanf("%d",&N);
printf("ENTER THE RATE OF INTEREST(%) : ");
scanf("%d",&R);
for(i=0;i<N;i++)
{
SI=P*R;
SI=SI/100;
P=P+SI;
CI=CI+SI;
}
printf("THE CALCULATED COMPOUND INTEREST IS RUPEES : %ld",CI);
getch();
}

N comes out of the SIMPLE interest formula and goes in the LOOP. Now this Loop should run for N times. This can be achieved by using for(i=0;i<N;i++){}

Explaination of FOR LOOP:

for (initialization;condition;increment or decrement){}

First part is initialization where we set the value so that we can check condition in Second part. Condition will always give you the result to be True or False. LOOP continues till the condition is True and Stops when condition is False. Third part will be for increment or decrement so that the value which is set has to change and at make the Condition False sometime in the go.

Let us understand the working of FOR LOOP.

Suppose 5 is entered by the User for N.

i=0 that means i is assigned a value Zero.

1st condition will be 0<5 which is True i++ means i=i+1

i=1 2nd condition will be 1<5 which is True i++ means i=i+1

i=2 3rd condition will be 2<5 which is True i++ means i=i+1

i=3 4th condition will be 3<5 which is True i++ means i=i+1

i=4 5th condition will be 4<5 which is True i++ means i=i+1

i=5 6th condition will be 5<5 which is False LOOP Terminates (stops).

So the code written inside the {} will be executed 5 times.

P=P+SI;
CI=CI+SI;

P is changed by adding SI (Simple Interest) to Principal Amount (each and every time the loop runs). CI is changed by adding SI (Simple Interest) to Compound Interest (each and every time the loop runs).

output:-

C_program_Compound_Interest

C_program_Compound_Interest_Output

video:-

Leave a Reply