Simple Interest: flowchart, algorithm and program

This program reads the principal (p), the rate (r) and the time (t) from the user, then prints the simple interest. It uses the formula si = p * r * t / 100, which is a direct calculation with no loops or conditions.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read p (principal).
  3. Step 3: Read r (rate).
  4. Step 4: Read t (time).
  5. Step 5: Set si = p * r * t / 100.
  6. Step 6: Print si.
  7. Step 7: Stop.

Pseudocode

BEGIN
    READ p (principal)
    READ r (rate)
    READ t (time)
    si ← p * r * t / 100
    PRINT si
END

Program in C, Python and Java

Simple Interest program in C

#include <stdio.h>

int main() {
    float p, r, t, si;
    printf("Enter principal: ");
    scanf("%f", &p);
    printf("Enter rate: ");
    scanf("%f", &r);
    printf("Enter time: ");
    scanf("%f", &t);
    si = p * r * t / 100;
    printf("Simple Interest = %.2f\n", si);
    return 0;
}

Simple Interest program in Python

p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = float(input("Enter time: "))
si = p * r * t / 100
print("Simple Interest = {:.2f}".format(si))

Simple Interest program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter principal: ");
        float p = sc.nextFloat();
        System.out.print("Enter rate: ");
        float r = sc.nextFloat();
        System.out.print("Enter time: ");
        float t = sc.nextFloat();
        float si = p * r * t / 100;
        System.out.printf("Simple Interest = %.2f%n", si);
    }
}

Sample input: p = 5000, r = 5, t = 2
Output: Enter principal: Enter rate: Enter time: Simple Interest = 250.00

How it works

  • Four float variables are declared: p for principal, r for rate, t for time and si for the result. Using float lets the user enter values like 7.5 or 1.5 as well as whole numbers.
  • Each value is read with scanf("%f", ...) after a prompt, so the order of input is principal, then rate, then time.
  • The line si = p * r * t / 100 multiplies the three values and divides by 100, because the rate is a percentage per year.
  • For p = 5000, r = 5, t = 2: 5000 * 5 = 25000, then 25000 * 2 = 50000, then 50000 / 100 = 250.
  • printf with %.2f prints si rounded to two decimal places, so the output is Simple Interest = 250.00.

Dry run (trace table)

StepLineBoxprtsiOutput
13Start
26Read p (principal)5000Enter principal:
38Read r (rate)50005Enter rate:
410Read t (time)500052Enter time:
511si = p * r * t / 100500052250
612Print si500052250Simple Interest = 250.00
713Stop500052250

Time and space complexity

Time: O(1) · Space: O(1). Only a fixed number of reads and arithmetic operations are done, with no loops.

Common mistakes

  • Forgetting the & in scanf, for example scanf("%f", p), which makes the program crash or behave unpredictably.
  • Leaving out the division by 100, which gives 50000 instead of 250 for the sample input.
  • Declaring the variables as int, which breaks decimal input and can cause wrong results; also, using %d instead of %f to read or print floats.

Viva questions

Q1. What does this program compute, and what is the formula used?

It computes simple interest using SI = P*R*T/100, where P is the principal, R the yearly rate in percent, and T the time in years. The result is printed on line 12.

Q2. Why did you declare p, r, t and si as float instead of int?

Money, rate and time can have decimal parts, such as 2.5 years or 7.5%. An int would reject the fractional part when reading and would truncate the result.

Q3. Why is there an & before p, r and t in scanf but not in printf?

scanf must write the typed value into the variable, so it needs the variable's address, which & gives. printf only reads the value, so it needs no address.

Q4. What does %.2f do in the printf statement?

It prints the float with exactly two digits after the decimal point, rounded. For example, 123.456 is shown as 123.46.

Q5. What happens if the user enters 0 or a negative number for principal, rate or time?

If any of them is 0, SI is 0. If one is negative, SI comes out negative, because the program does no validation. In a real program you should check that the inputs are positive and ask again if not.

Q6. If p, r and t were all int, what problem could arise in the line si = p * r * t / 100?

The expression would use integer division, so the fractional part would be cut off. For example, 150 / 100 gives 1 instead of 1.5. Using 100.0 or float variables avoids this.

Q7. What is the time complexity of this program?

It is O(1), constant time. There are no loops, only a fixed number of reads, one calculation and one print.

Q8. What would you change to compute compound interest instead?

Use A = P * (1 + R/100)^T and subtract P to get the interest. This needs pow() from math.h, and the program would need to be linked with -lm on some compilers.

Frequently asked questions

What is the formula for simple interest?

Simple interest = (principal * rate * time) / 100, where rate is the percent per year and time is in years. The total amount to be paid back is principal + interest.

Why use float instead of int in this program?

With int, values like a rate of 7.5% could not be entered, and the result could lose its decimal part. Float handles fractional input and output correctly.

What does %.2f mean in printf?

It prints a floating-point number with exactly two digits after the decimal point, so 250 is shown as 250.00.

More programs

← All programs

Try AI Flowchart free →