0
0
CProgramBeginner · 2 min read

C Program to Calculate Simple Interest with Example

A simple C program to calculate simple interest uses the formula simple_interest = (principal * rate * time) / 100 and prints the result.
📋

Examples

Inputprincipal=1000, rate=5, time=2
OutputSimple Interest = 100.00
Inputprincipal=1500, rate=4.5, time=3
OutputSimple Interest = 202.50
Inputprincipal=0, rate=10, time=5
OutputSimple Interest = 0.00
🧠

How to Think About It

To calculate simple interest, first get the principal amount, the rate of interest per year, and the time in years. Then multiply these three values and divide by 100 to get the interest. Finally, display the result.
📐

Algorithm

1
Get input values for principal, rate, and time.
2
Calculate simple interest using the formula: (principal * rate * time) / 100.
3
Print the calculated simple interest.
💻

Code

c
#include <stdio.h>

int main() {
    float principal, rate, time, simple_interest;
    printf("Enter principal amount: ");
    scanf("%f", &principal);
    printf("Enter rate of interest: ");
    scanf("%f", &rate);
    printf("Enter time in years: ");
    scanf("%f", &time);

    simple_interest = (principal * rate * time) / 100;

    printf("Simple Interest = %.2f\n", simple_interest);
    return 0;
}
🔍

Dry Run

Let's trace the example where principal=1000, rate=5, and time=2 through the code.

1

Input principal

User enters 1000, so principal = 1000

2

Input rate

User enters 5, so rate = 5

3

Input time

User enters 2, so time = 2

4

Calculate simple interest

simple_interest = (1000 * 5 * 2) / 100 = 100

5

Print result

Output: Simple Interest = 100.00

StepVariableValue
1principal1000
2rate5
3time2
4simple_interest100
💡

Why This Works

Step 1: Getting inputs

The program asks the user to enter the principal, rate, and time, storing them in variables.

Step 2: Calculating interest

It uses the formula (principal * rate * time) / 100 to find the simple interest.

Step 3: Displaying output

Finally, it prints the calculated interest with two decimal places for clarity.

🔄

Alternative Approaches

Using integer inputs and output
c
#include <stdio.h>

int main() {
    int principal, time;
    float rate, simple_interest;
    printf("Enter principal amount: ");
    scanf("%d", &principal);
    printf("Enter rate of interest: ");
    scanf("%f", &rate);
    printf("Enter time in years: ");
    scanf("%d", &time);

    simple_interest = (principal * rate * time) / 100;

    printf("Simple Interest = %.2f\n", simple_interest);
    return 0;
}
This approach uses integer input for principal and time but float for rate, allowing fractional rates but whole number principal and time.
Using command line arguments
c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc != 4) {
        printf("Usage: %s principal rate time\n", argv[0]);
        return 1;
    }
    float principal = atof(argv[1]);
    float rate = atof(argv[2]);
    float time = atof(argv[3]);
    float simple_interest = (principal * rate * time) / 100;
    printf("Simple Interest = %.2f\n", simple_interest);
    return 0;
}
This method takes inputs from command line arguments, useful for quick calculations without interactive input.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic operations and input/output steps, so it runs in constant time.

Space Complexity

It uses a fixed number of variables, so the space used is constant.

Which Approach is Fastest?

All approaches run in constant time and space; differences are mainly in input method and data types.

ApproachTimeSpaceBest For
Standard input with floatsO(1)O(1)Interactive use with decimal inputs
Integer inputs for principal/timeO(1)O(1)When principal and time are whole numbers
Command line argumentsO(1)O(1)Quick calculations without prompts
💡
Always use float or double for rate and time to handle decimal values accurately.
⚠️
Beginners often forget to divide by 100 in the formula, leading to incorrect interest calculation.