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


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

  1. #include<stdio.h>
  2. void main()
  3. {
  4. int N,R;
  5. long int SI=0,P;
  6. clrscr();
  8. scanf("%ld",&P);
  9. printf("ENTER THE NUMBER OF YEAR(S) : ");
  10. scanf("%d",&N);
  11. printf("ENTER THE RATE OF INTEREST(%) : ");
  12. scanf("%d",&R);
  13. SI=P*R*N;
  14. SI=SI/100;
  16. getch();}

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.





Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.