Introduction
In this lab, you will learn how to compute the present value of a future amount using C programming. The lab covers the essential steps, including reading the financial parameters (future value, interest rate, and time period), and then applying the present value formula to calculate the present value. The lab provides a step-by-step guide with code examples to help you understand and implement the present value calculation in C.
The lab demonstrates how to use the scanf() function to read user inputs, store them as double-precision floating-point numbers, and then perform the present value calculation using the formula: Present Value (PV) = Future Value (FV) / (1 + Rate)^Time. The calculated present value is then printed to the console, allowing you to see the result of the computation.
Read Future Value, Rate, Time
In this step, you will learn how to read the essential financial parameters for calculating the present value: Future Value, Interest Rate, and Time Period.
First, create a new C file for the present value calculation:
cd ~/project
nano present_value.c
Now, let's write the code to read input values:
#include <stdio.h>
#include <math.h>
int main() {
double future_value, rate, time;
printf("Enter Future Value: ");
scanf("%lf", &future_value);
printf("Enter Annual Interest Rate (in decimal): ");
scanf("%lf", &rate);
printf("Enter Time Period (in years): ");
scanf("%lf", &time);
printf("Inputs:\n");
printf("Future Value: %.2f\n", future_value);
printf("Interest Rate: %.2f%%\n", rate * 100);
printf("Time Period: %.2f years\n", time);
return 0;
}
Compile the program:
gcc -o present_value present_value.c -lm
Example output when running the program:
Enter Future Value: 1000
Enter Annual Interest Rate (in decimal): 0.05
Enter Time Period (in years): 3
Inputs:
Future Value: 1000.00
Interest Rate: 5.00%
Time Period: 3.00 years
This code demonstrates how to:
- Use
scanf()to read user inputs for future value, interest rate, and time period - Store inputs as double-precision floating-point numbers
- Print the entered values to confirm correct input
PV = FV/(1+R)^T
In this step, you will implement the present value calculation using the financial formula: Present Value (PV) = Future Value (FV) / (1 + Rate)^Time.
Modify the previous present_value.c file to include the calculation:
nano ~/project/present_value.c
Update the code with the present value calculation:
#include <stdio.h>
#include <math.h>
int main() {
double future_value, rate, time, present_value;
printf("Enter Future Value: ");
scanf("%lf", &future_value);
printf("Enter Annual Interest Rate (in decimal): ");
scanf("%lf", &rate);
printf("Enter Time Period (in years): ");
scanf("%lf", &time);
// Calculate Present Value
present_value = future_value / pow((1 + rate), time);
printf("\nCalculation Results:\n");
printf("Future Value: %.2f\n", future_value);
printf("Interest Rate: %.2f%%\n", rate * 100);
printf("Time Period: %.2f years\n", time);
printf("Present Value: %.2f\n", present_value);
return 0;
}
Compile and run the updated program:
gcc -o present_value present_value.c -lm
./present_value
Example output:
Enter Future Value: 1000
Enter Annual Interest Rate (in decimal): 0.05
Enter Time Period (in years): 3
Calculation Results:
Future Value: 1000.00
Interest Rate: 5.00%
Time Period: 3.00 years
Present Value: 862.07
Key points in this code:
- Use
pow()function from<math.h>to calculate (1 + rate)^time - Implement the present value formula: PV = FV / (1 + R)^T
- Display detailed calculation results
Print Present Value
In this step, you will enhance the present value calculation program by adding formatted output and error handling to improve user experience.
Update the present_value.c file with improved formatting and validation:
nano ~/project/present_value.c
Modify the code to include better output formatting:
#include <stdio.h>
#include <math.h>
int main() {
double future_value, rate, time, present_value;
// Input validation
printf("Present Value Calculator\n");
printf("----------------------\n");
printf("Enter Future Value (must be positive): ");
if (scanf("%lf", &future_value) != 1 || future_value <= 0) {
printf("Error: Invalid future value. Please enter a positive number.\n");
return 1;
}
printf("Enter Annual Interest Rate (in decimal, e.g., 0.05 for 5%%): ");
if (scanf("%lf", &rate) != 1 || rate < 0) {
printf("Error: Invalid interest rate. Please enter a non-negative number.\n");
return 1;
}
printf("Enter Time Period (in years, must be positive): ");
if (scanf("%lf", &time) != 1 || time <= 0) {
printf("Error: Invalid time period. Please enter a positive number.\n");
return 1;
}
// Calculate Present Value
present_value = future_value / pow((1 + rate), time);
// Formatted output
printf("\n--- Present Value Calculation ---\n");
printf("Future Value: $%.2f\n", future_value);
printf("Annual Rate: %.2f%%\n", rate * 100);
printf("Time Period: %.2f years\n", time);
printf("Present Value: $%.2f\n", present_value);
return 0;
}
Compile and run the updated program:
gcc -o present_value present_value.c -lm
./present_value
Example output:
Present Value Calculator
----------------------
Enter Future Value (must be positive): 1000
Enter Annual Interest Rate (in decimal, e.g., 0.05 for 5%): 0.05
Enter Time Period (in years, must be positive): 3
--- Present Value Calculation ---
Future Value: $1000.00
Annual Rate: 5.00%
Time Period: 3.00 years
Present Value: $862.07
Key improvements:
- Added input validation to prevent invalid inputs
- Enhanced output formatting with clear labels
- Improved error handling for user inputs
- Added descriptive prompts and calculation summary
Summary
In this lab, you will learn how to read the essential financial parameters for calculating the present value, including Future Value, Interest Rate, and Time Period. You will then implement the present value calculation using the financial formula: Present Value (PV) = Future Value (FV) / (1 + Rate)^Time. The lab demonstrates how to use C programming to prompt the user for input, perform the calculation, and display the resulting present value.



