THE SYLLABUS QUESTION
What you need to solve
Write the program for the simple, compound interest.
Principal, annual percentage rate, years, and compounding periods per year. Principal: 0–1,000,000,000; rate: 0–100%; years: 0–100; periods: 1–365. Example: 1000 10 2 1.
UNDERSTAND THE IDEA
Explanation
Simple interest is P × R × T / 100. Compound amount is P × (1 + R / (100 × m))^(m × T), where m is the number of compounding periods per year.
Compound interest is the compound amount minus the original principal. The program uses double and pow from math.h, then rounds only for display.
An annual period count of 1 gives the familiar annual-compounding formula. These are classroom calculations with a fixed rate and no deposits or fees.
PLAN BEFORE CODING
Algorithm
- Read principal, rate, time and compounding frequency.
- Validate the supported ranges.
- Calculate simple interest and its final amount.
- Calculate compound amount with pow, then subtract principal.
- Print interest and final amount to two decimal places.
SEE THE CONTROL FLOW
Flowchart
Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.
On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗
#include <stdio.h>
#include <math.h>
int main(void) {
double principal, rate, years, simple, compound_amount;
int periods;
if (scanf("%lf %lf %lf %d", &principal, &rate, &years, &periods) != 4 ||
!isfinite(principal) || !isfinite(rate) || !isfinite(years) ||
principal < 0 || principal > 1000000000.0 ||
rate < 0 || rate > 100 || years < 0 || years > 100 ||
periods < 1 || periods > 365) {
puts("Invalid input.");
return 1;
}
simple = principal * rate * years / 100.0;
compound_amount = principal * pow(1.0 + rate / (100.0 * periods),
periods * years);
printf("Simple interest: %.2f\n", simple);
printf("Simple amount: %.2f\n", principal + simple);
printf("Compound interest: %.2f\n", compound_amount - principal);
printf("Compound amount: %.2f\n", compound_amount);
return 0;
}
Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.
Compile and run locally
gcc -std=c17 simple-compound-interest.c -o lab -lm
./labOn Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.
FOLLOW THE VALUES
Dry run
| Step / state | Operation | Result |
|---|---|---|
| P = 1000, R = 10, T = 2, m = 1 | Simple interest | 1000 × 10 × 2 / 100 = 200 |
| Compound factor | (1 + 10/100)^2 | 1.21 |
| Compound amount and interest | 1000 × 1.21; subtract 1000 | 1210; interest = 210 |
CHECK THE BEHAVIOR
Sample input & output
Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.
Sample 1
1000 10 2 1
Simple interest: 200.00
Simple amount: 1200.00
Compound interest: 210.00
Compound amount: 1210.00
Sample 2
5000 0 3 4
Simple interest: 0.00
Simple amount: 5000.00
Compound interest: 0.00
Compound amount: 5000.00
Sample 3
1000 12 1 12
Simple interest: 120.00
Simple amount: 1120.00
Compound interest: 126.83
Compound amount: 1126.83
Common mistakes
- Use 100.0 so percentage calculations use floating-point arithmetic.
- Link the math library with -lm when your compiler requires it.
WHY THIS GROWTH RATE?
Time and space complexity
Fixed number of arithmetic operations and one pow call; auxiliary space O(1).
The program evaluates two formulas with a fixed number of arithmetic operations and one pow call. There is no loop that runs once per year or per compounding period; the exponent is passed directly to pow.
In the usual introductory model, fixed-width arithmetic and one math-library call are treated as constant-cost operations, giving O(1) time and O(1) auxiliary space. The internal implementation of pow is library-dependent; this estimate does not analyze arbitrary-precision exponentiation.
Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.