In this step, you will learn how to read the principal amount, interest rate, and time period for calculating compound interest in a C program. We'll create a simple program that takes user input for these financial parameters.
First, let's create a new C file in the project directory:
cd ~/project
nano compound_interest.c
Now, add the following code to read the input parameters:
#include <stdio.h>
int main() {
// Declare variables for principal, rate, and time
float principal, rate, time;
// Prompt and read principal amount
printf("Enter the principal amount: ");
scanf("%f", &principal);
// Prompt and read interest rate
printf("Enter the annual interest rate (in percentage): ");
scanf("%f", &rate);
// Prompt and read time period
printf("Enter the time period (in years): ");
scanf("%f", &time);
// Print the input values to verify
printf("\nInput Parameters:\n");
printf("Principal Amount: $%.2f\n", principal);
printf("Annual Interest Rate: %.2f%%\n", rate);
printf("Time Period: %.2f years\n", time);
return 0;
}
Compile and run the program:
gcc compound_interest.c -o compound_interest
./compound_interest
Example output:
Enter the principal amount: 1000
Enter the annual interest rate (in percentage): 5
Enter the time period (in years): 3
Input Parameters:
Principal Amount: $1000.00
Annual Interest Rate: 5.00%
Time Period: 3.00 years
float
variables to store decimal values for financial calculationsprintf()
is used to display prompts and instructions to the userscanf()
reads user input for principal, rate, and time%.2f
format specifier displays numbers with two decimal placesIn this step, you will learn how to calculate the compound interest using the mathematical formula A = P * (1 + R)^T, where:
Let's modify the previous program to include the compound interest calculation:
cd ~/project
nano compound_interest.c
Update the program with the compound interest calculation:
#include <stdio.h>
#include <math.h>
int main() {
// Declare variables for principal, rate, time, and amount
float principal, rate, time, amount;
// Prompt and read principal amount
printf("Enter the principal amount: ");
scanf("%f", &principal);
// Prompt and read annual interest rate
printf("Enter the annual interest rate (in percentage): ");
scanf("%f", &rate);
// Prompt and read time period
printf("Enter the time period (in years): ");
scanf("%f", &time);
// Convert rate to decimal
rate = rate / 100;
// Calculate compound interest
amount = principal * pow(1 + rate, time);
// Print the results
printf("\nInput Parameters:\n");
printf("Principal Amount: $%.2f\n", principal);
printf("Annual Interest Rate: %.2f%%\n", rate * 100);
printf("Time Period: %.2f years\n", time);
printf("\nCompound Interest Calculation:\n");
printf("Final Amount: $%.2f\n", amount);
printf("Total Interest Earned: $%.2f\n", amount - principal);
return 0;
}
Compile the program with the math library:
gcc compound_interest.c -o compound_interest -lm
Run the program:
./compound_interest
Example output:
Enter the principal amount: 1000
Enter the annual interest rate (in percentage): 5
Enter the time period (in years): 3
Input Parameters:
Principal Amount: $1000.00
Annual Interest Rate: 5.00%
Time Period: 3.00 years
Compound Interest Calculation:
Final Amount: $1157.63
Total Interest Earned: $157.63
#include <math.h>
is added to use the pow()
function for exponentiationrate = rate / 100
converts percentage to decimalamount = principal * pow(1 + rate, time)
calculates the final amount-lm
flag is used when compiling to link the math libraryIn this final step, you will enhance the compound interest calculation program by adding formatted output and improving the presentation of financial results.
Let's modify the previous program to improve result formatting:
cd ~/project
nano compound_interest.c
Update the program with enhanced output formatting:
#include <stdio.h>
#include <math.h>
int main() {
// Declare variables for principal, rate, time, and amount
float principal, rate, time, amount, interest;
// Prompt and read principal amount
printf("Compound Interest Calculator\n");
printf("----------------------------\n");
printf("Enter the principal amount: ");
scanf("%f", &principal);
// Prompt and read annual interest rate
printf("Enter the annual interest rate (in percentage): ");
scanf("%f", &rate);
// Prompt and read time period
printf("Enter the time period (in years): ");
scanf("%f", &time);
// Convert rate to decimal
rate = rate / 100;
// Calculate compound interest
amount = principal * pow(1 + rate, time);
interest = amount - principal;
// Print formatted financial results
printf("\n===== Financial Calculation Results =====\n");
printf("Initial Principal: $%10.2f\n", principal);
printf("Annual Interest Rate: %10.2f%%\n", rate * 100);
printf("Investment Period: %10.2f years\n", time);
printf("-------------------------------------------\n");
printf("Final Amount: $%10.2f\n", amount);
printf("Total Interest Earned: $%10.2f\n", interest);
printf("==========================================\n");
return 0;
}
Compile the program:
gcc compound_interest.c -o compound_interest -lm
Run the program:
./compound_interest
Example output:
Compound Interest Calculator
----------------------------
Enter the principal amount: 1000
Enter the annual interest rate (in percentage): 5
Enter the time period (in years): 3
===== Financial Calculation Results =====
Initial Principal: $ 1000.00
Annual Interest Rate: 5.00%
Investment Period: 3.00 years
-------------------------------------------
Final Amount: $ 1157.63
Total Interest Earned: $ 157.63
==========================================
%10.2f
format specifier for aligned, fixed-width decimal outputIn this lab, you learned how to read the principal amount, interest rate, and time period for calculating compound interest in a C program. You created a simple program that prompts the user for these financial parameters and prints the input values to verify. Next, you will learn how to calculate the compound interest using the mathematical formula A = P * (1 + R)^T, where A is the final amount, P is the principal amount, R is the interest rate, and T is the time period.