C Program to Find Simple Interest
C Program to Find Simple Interest
A school exercise asks you to simple interest. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Simple interest applies a fixed rate to the original principal for each period, without adding earlier interest to the calculation base.
Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why simple interest uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use three years at the same annual rate.
Simple Interest: follow the data
Given data
A school exercise asks you to simple interest. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Rule
Simple interest applies a fixed rate to the original principal for each period, without adding earlier interest to the calculation base.
Observed result
Interest: 100.00
Understand the operation
For a classroom budget model, five percent means 5/100, not a multiplier of five. Time must use the same period unit as the stated rate.
Read the declarations and operands before the output call. %d formats an int; floating-point results use an appropriate precision with %f. A format describes a value that was already computed—it does not choose the earlier arithmetic.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
double principal = 1000.0, percent = 5.0, years = 2.0;
double interest = principal * percent * years / 100.0;
printf("Interest: %.2f\n", interest);
return 0;
}
Expected output
Interest: 100.00
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
double principal = 1000.0, percent = 5.0, years = 2.0;
double interest = principal * percent * years / 100.0;
printf("Interest: %.2f\n", interest);
return 0;
}
1000 × 5 × 2 is 10000.
Dividing by 100 converts the percentage and yields interest 100.
Follow the worked calculation
Start with the sample
1000 × 5 × 2 is 10000.
Apply the rule
Dividing by 100 converts the percentage and yields interest 100.
Check the result
Interest: 100.00
Interest: 100.00
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
double principal = 1000.0, percent = 5.0, years = 2.0;
double interest = principal * percent * years / 100.0;
printf("Interest: %.2f\n", interest);
return 0;
}
Use three years at the same annual rate.
Worked answer
The worked answer follows the same rule with the requested changed data. Compare its complete source and output: Interest: 150.00. The result should follow from the operations, rather than from memorizing the first sample.
C17 · complete program
#include <stdio.h>
int main(void) {
double principal = 1000.0, percent = 5.0, years = 3.0;
double interest = principal * percent * years / 100.0;
printf("Interest: %.2f\n", interest);
return 0;
}
Expected output
Interest: 150.00
Run in C compiler →Recognize the important boundaries
This is a simplified arithmetic exercise. It excludes changing rates, fees and exact-decimal accounting.
The demonstrated values stay within the types’ representable ranges. A program accepting arbitrary external values needs checked conversion and bounds before performing arithmetic.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Simple interest applies a fixed rate to the original principal for each period, without adding earlier interest to the calculation base.
Continue with arithmetic operand types.
Continue with validated input.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.