Practical 1(a): Simple Interest
Chapter Four
Syllabus topic Module 1, Practical 1(a): "To calculate simple interest taking principal, rate of interest and number of years as input from user. Write algorithm & draw flowchart for the same."
Pages 13 to 16 of 206
Aim
To calculate simple interest, taking the principal, the rate of interest and the number of years as input from the user, and to write the algorithm and draw the flowchart for the same.
The formula, and where it comes from
Simple interest is interest paid on the original sum only, never on interest already earned.
SI = (P R N) / 100
P is the principal, the money borrowed or deposited. R is the rate per cent per year. N is the number of years. The division by 100 is there because R is given as a percentage: 8.5 per cent means 8.5 hundredths, and dividing by 100 turns the percentage into the fraction the arithmetic actually needs.
One worked case by hand, so you know what the program should print. Principal 12000, rate 8.5, three years. 12000 multiplied by 8.5 is 102000. Multiplied by 3 is 306000. Divided by 100 is 3060.
Algorithm
1. Start
2. Read P, R and N
3. Set SI to (P into R into N) divided by 100
4. Print SI
5. Stop
Flowchart
Figure 4.1 Practical 1(a): five steps, no decisions and no loops
Five boxes and four arrows. There is no diamond in this chart, because nothing in the problem depends on a condition, and no arrow returns upwards, because nothing repeats. That is what a straight line program looks like.
The program
#include <stdio.h>
int main(void)
{
float p, r, n, si;
printf("Enter the principal: ");
scanf("%f", &p);
printf("Enter the rate of interest: ");
scanf("%f", &r);
printf("Enter the number of years: ");
scanf("%f", &n);
si = (p * r * n) / 100;
printf("Principal = %.2f\n", p);
printf("Rate = %.2f percent\n", r);
printf("Years = %.2f\n", n);
printf("Simple interest = %.2f\n", si);
return 0;
}12000
8.5
3Enter the principal: Enter the rate of interest: Enter the number of years: Principal = 12000.00
Rate = 8.50 percent
Years = 3.00
Simple interest = 3060.00The run above was fed 12000, 8.5 and 3. Your screen puts each of those numbers after its own prompt as you type it; the block here is only what the program itself printed, for the reason given in [Your First Program: Writing It, Compiling It and Running It].
The program, line by line
float p, r, n, si; declares four variables. A variable is a named place in memory that holds a value you can change. float is the type: a number that may have a fractional part. A rate of 8.5 per cent is not a whole number, and neither is most interest, so float and not int.
Every variable in C is declared before it is used, and the declaration says what type it is. That is not bureaucracy: the type is how the machine knows how many bytes to set aside and how to interpret them.
Practical 1(a): Simple Interest
printf("Enter the principal: "); prints the prompt. There is no \n at the end on purpose, so that the cursor stays on the same line and the number you type appears next to the words.
scanf("%f", &p); reads one number from the keyboard and stores it in p.
Two parts of that line deserve a paragraph each, because they are where first-semester marks are lost.
"%f" is the format specifier, and it tells scanf what kind of value to expect. %f is a float, %d a whole number, %c a single character, %s a string. Give scanf the wrong one and it reads the bytes wrongly; there is no error message.
&p is the address of p, and the & is not optional. scanf has to change the variable, and to change something it needs to be told where it lives, not what is currently in it. Leaving the & out compiles on many compilers with a warning and then behaves unpredictably at run time. There is one exception you will meet later: a string read with %s needs no &, because the name of an array already is an address.
si = (p r n) / 100; is the calculation. The is multiplication, / is division, and the brackets say do the multiplying first. The brackets are not needed here, because and / have the same precedence and run left to right, but they are worth writing: they make the formula look like the formula.
printf("Simple interest = %.2f\n", si); prints. Inside a printf the % markers are filled in from the values listed after the string, in order. %.2f means a float rounded to two decimal places, which is what money wants.
The trap: integer division
Declare everything int instead and the program goes wrong in a way nothing warns you about. Take a principal of 1250 at 7 per cent for 3 years. By hand: 1250 by 7 is 8750, by 3 is 26250, divided by 100 is 262.5.
#include <stdio.h>
int main(void)
{
int p = 1250, r = 7, n = 3;
printf("Integer division: %d\n", (p * r * n) / 100);
printf("Floating point: %.2f\n", (float) p * r * n / 100);
return 0;
}Integer division: 262
Floating point: 262.50Half a rupee has gone, and no compiler said a word. When both sides of a / are whole numbers C performs integer division: it throws the fractional part away rather than rounding it. 26250 divided by 100 is 262 with a remainder of 50, and the remainder is simply dropped.
Practical 1(a): Simple Interest
The second line shows the fix when you cannot change the declarations. (float) p is a cast, which converts that one value to a float for this one expression. As soon as one side of an operator is a float, C converts the other side too and the whole calculation is done in floating point.
The worse version of the same trap is at the other end, when the value is read rather than computed:
#include <stdio.h>
int main(void)
{
int rate = 8.5;
printf("You meant 8.5, and the int is holding %d\n", rate);
return 0;
}You meant 8.5, and the int is holding 8The fraction is gone before any arithmetic happens at all, so every figure the program prints afterwards is wrong by six per cent. Here the compiler does warn, because the constant 8.5 is visible in the source; when the 8.5 arrives through scanf into an int there is nothing in the source to warn about and the loss is silent. That is why the working program above declares all four variables float.
What beginners get wrong
Leaving out the & in scanf. The variable is not filled in and the value you see is whatever was in memory.
Using %d for a float, or %f for an int. The compiler warns with -Wall, and the printed value is nonsense. Match the specifier to the type every time.
Declaring the rate as int. A rate of 8.5 becomes 8, silently.
Writing si = p r n / 100 with everything int and believing the answer. Integer division truncates; it does not round.
Forgetting that printf prompts have no newline. That is deliberate here, but if you copy a prompt with \n into a program that reads three values, the layout looks wrong next to the journal.
Quick revision
SI = (P R N) / 100, and the 100 is there because R is a percentage.floatfor anything with a fraction;intonly for whole numbers.scanf("%f", &p): format specifier, and the&is the address.%dint,%ffloat,%ccharacter,%sstring.%.2fprints two decimal places.- Integer divided by integer is integer division: the remainder is discarded, not rounded.
- A cast
(float) pforces floating point arithmetic. - 12000 at 8.5 per cent for 3 years is 3060.
What goes in your journal
Aim, the five-step algorithm, the flowchart with its five boxes, the program, the output with your own values written in, and a conclusion naming the one thing the practical demonstrated: that the rate and the result must be held in a floating point type or the division throws the fraction away.
Practical 1(a): Simple Interest
Test yourself
1. Why is & needed in scanf("%f", &p) but not in printf("%f", p)? scanf must change p, so it needs the address of p. printf only reads the value, so the value itself is enough.
2. int a = 7, b = 2; What does a / b give, and why?
- Both operands are integers, so C performs integer division and discards the fractional part rather than rounding it.
3. What does %.2f do? Prints a floating point value rounded to two places after the decimal point.
4. You typed 8.5 for the rate but the interest came out as though the rate were 8. What is the likeliest cause? The rate was declared int, so the fractional part was lost when the value was stored.
5. Rewrite step 3 of the algorithm for compound interest. Set CI to P into ((1 plus R divided by 100) raised to N) minus P. The reading and printing steps do not change.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself for the same subject.