C Program to Calculate Total Amount of Money
This guide shows you how to write a C program to calculate the total amount of money, including both simple and more advanced versions. We'll cover the basic program structure, input handling, calculations, and output formatting.
Introduction
Calculating the total amount of money is a fundamental programming task that demonstrates basic input/output operations and arithmetic calculations in C. This guide will walk you through creating a C program that:
- Prompts the user for principal amount, interest rate, and time period
- Calculates the total amount using simple interest or compound interest formulas
- Displays the result with proper formatting
The programs we'll create will help you understand how financial calculations work in C and provide a foundation for more complex financial applications.
Basic C Program to Calculate Total Amount
Here's a simple C program that calculates the total amount using simple interest:
int main() {
float principal, rate, time, simple_interest, total_amount;
// Input from user
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter interest rate (per year): ");
scanf("%f", &rate);
printf("Enter time period (in years): ");
scanf("%f", &time);
// Calculate simple interest
simple_interest = (principal * rate * time) / 100;
// Calculate total amount
total_amount = principal + simple_interest;
// Display result
printf("Simple Interest: %.2f\n", simple_interest);
printf("Total Amount: %.2f\n", total_amount);
return 0;
}
Formula Used
The simple interest formula is:
Simple Interest = (Principal × Rate × Time) / 100
Total Amount = Principal + Simple Interest
Advanced C Program with Compound Interest
This more advanced version calculates the total amount using compound interest:
#include <math.h>
int main() {
float principal, rate, time, compound_amount;
// Input from user
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter annual interest rate: ");
scanf("%f", &rate);
printf("Enter time period (in years): ");
scanf("%f", &time);
// Calculate compound amount
compound_amount = principal * pow((1 + rate/100), time);
// Display result
printf("Total Amount with Compound Interest: %.2f\n", compound_amount);
return 0;
}
Formula Used
The compound interest formula is:
A = P × (1 + r/100)^t
Where:
- A = Amount after time t
- P = Principal amount
- r = Annual interest rate
- t = Time in years
Worked Example
Let's walk through an example calculation using the simple interest program:
Example Calculation
Principal amount: $1000
Interest rate: 5% per year
Time period: 3 years
Calculation:
Simple Interest = (1000 × 5 × 3) / 100 = $150
Total Amount = $1000 + $150 = $1150
For the compound interest version with the same inputs:
Example Calculation
Principal amount: $1000
Interest rate: 5% per year
Time period: 3 years
Calculation:
A = 1000 × (1 + 0.05)^3 ≈ $1157.63
FAQ
What is the difference between simple and compound interest?
Simple interest is calculated only on the original principal amount, while compound interest is calculated on the initial principal and also on the accumulated interest of previous periods. Compound interest typically results in higher returns over time.
How do I compile and run these C programs?
You'll need a C compiler like GCC installed. Save the code to a file (e.g., interest.c), then compile with "gcc interest.c -o interest -lm" and run with "./interest".
Can I modify these programs to handle different currencies?
Yes, you can add currency symbols to the output by modifying the printf statements. For example: printf("Total Amount: $%.2f\n", total_amount);
What if I want to calculate interest for monthly payments?
You would need to adjust the formula to account for monthly compounding. The formula would become A = P × (1 + r/(100×12))^(t×12).