An interactive C program to calculate 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 decide the data type. int, float, and double are the choices for the variable to be used, since we are using only whole number and not the fraction values of it. Hence we will omit float and double. Select int as our data type.

But there will be a problem doing so. Because the limit of int has values between -32768 to +32767. Therefore we will be able to calculate the interest for the principal amount not greater than +32767. For more details of ranges of data types for storage memory space please click here.

Now to increase the range from +32767 to +65535, you have to use unsigned data type qualifier. But this will also restrict the principal amount maximum to +65535 only.

Best choice for principal amount is long Data type Qualifier which has the value range from -2147483648 to +2147483647. So now this is good enough.

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

long int SI,P; int N,R; 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. (Eg. ENTER THE PRINCIPAL AMOUNT : ) than scan the value entered for principal in P. and so on.

SI = (P * N * R)/100;

This statement does not work so divide this equation into two statement. i.e. SI = P * N * R; SI = SI / 100;

This statements work fine.

SI is initialized to zero so that garbage value (fake value) which is their inside variable is cleared and set to zero value.

Note:- Remember whenever you are scanning or printing the values of long int be careful to write %ld instead of just %d. Because %ld is used for only long int data type qualifier and %d is used for other unsigned int, signed int and short int data type qualifiers.

C program code
[codesyntax lang="c" lines="normal"]

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

[/codesyntax]

scanf(“%d”,&N); – explained

scanf() stands for Scan Function. The value to be scanned from the keyboard which is seen on screen is entered by the user. Inside double quotes we have %d which is a format specifier and it tells which type of variable is expected. d stands for decimal. After comma & comes. , is a separator. & stands for Address of and tells us where to save the value entered by the user and N is variable were the value will be saved.

output:-

C_program_Simple_Interest

C_program_Simple_Interest_Output

video:-

Leave a Reply