C Program to Calculate Simple Interest with Example
simple_interest = (principal * rate * time) / 100 and prints the result.Examples
How to Think About It
Algorithm
Code
#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.
Input principal
User enters 1000, so principal = 1000
Input rate
User enters 5, so rate = 5
Input time
User enters 2, so time = 2
Calculate simple interest
simple_interest = (1000 * 5 * 2) / 100 = 100
Print result
Output: Simple Interest = 100.00
| Step | Variable | Value |
|---|---|---|
| 1 | principal | 1000 |
| 2 | rate | 5 |
| 3 | time | 2 |
| 4 | simple_interest | 100 |
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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Standard input with floats | O(1) | O(1) | Interactive use with decimal inputs |
| Integer inputs for principal/time | O(1) | O(1) | When principal and time are whole numbers |
| Command line arguments | O(1) | O(1) | Quick calculations without prompts |