Read Payment, Rate, Number of Periods
In this step, you will learn how to read the key financial parameters needed to calculate the future value of an annuity in C. We'll create a program that prompts the user to input the periodic payment amount, interest rate, and number of periods.
First, let's create a new C file for our financial calculation:
cd ~/project
nano annuity_future_value.c
Now, enter the following code to read the financial parameters:
#include <stdio.h>
int main() {
double payment, rate;
int periods;
// Prompt user for input
printf("Enter periodic payment amount: ");
scanf("%lf", &payment);
printf("Enter annual interest rate (as a decimal): ");
scanf("%lf", &rate);
printf("Enter number of periods: ");
scanf("%d", &periods);
// Display the input values
printf("\nInput Parameters:\n");
printf("Periodic Payment: $%.2f\n", payment);
printf("Annual Interest Rate: %.2f%%\n", rate * 100);
printf("Number of Periods: %d\n", periods);
return 0;
}
Compile and run the program:
gcc annuity_future_value.c -o annuity_future_value
./annuity_future_value
Example output:
Enter periodic payment amount: 1000
Enter annual interest rate (as a decimal): 0.05
Enter number of periods: 10
Input Parameters:
Periodic Payment: $1000.00
Annual Interest Rate: 5.00%
Number of Periods: 10
Let's break down the code:
- We use
double
for payment and rate to handle decimal values
scanf()
is used to read user inputs for payment, rate, and periods
%lf
format specifier is used for reading double-precision floating-point numbers
%d
is used for reading integer values
- We print the input values to confirm the user's entries