C++ for Finance Basics
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Finance Lens
From C++ syntax to finance thinking
In Introduction to C++, you built the foundation: variables, types, operators, control flow, and functions. Now we’ll use those tools to express core finance ideas in code.
Finance programming often comes down to a few repeatable questions:
- What is money worth later?
- What is money worth today?
- How do we compare cash flows across time?
- How do we represent rates clearly and safely?
Think of this lesson as moving from “I can write C++” to “I can make C++ answer financial questions.” Later, we’ll use these basics in trading, risk, and data analysis.

Money Variables
Representing financial quantities
In finance code, names matter. A variable like x might be fine in a tiny math example, but production finance code benefits from explicit names:
-
principal: starting amount -
annualRate: yearly rate as a decimal -
years: investment horizon -
interest: amount earned or paid
For basic calculations, double is common because rates and prices are fractional. In real trading or accounting systems, money precision gets more nuanced, but double is enough for today’s finance basics.
Here’s a simple interest calculation. This is like the most basic version of “money grows over time”: no compounding yet, just linear growth.
```cpp
#include <iostream>
#include <iomanip>
int main() {
double principal = 10000.0; // $10,000
double annualRate = 0.05; // 5% as a decimal
double years = 3.0;
double interest = principal * annualRate * years;
double endingValue = principal + interest;
std::cout << std::fixed << std::setprecision(2);
std::cout << "Principal: $" << principal << "\n";
std::cout << "Interest earned: $" << interest << "\n";
std::cout << "Ending value: $" << endingValue << "\n";
return 0;
}
```
Rate Format
A C++ finance function expects an annual interest rate as a decimal.
Which value should represent 7.5%?
Compounding Growth
Time value of money
A dollar today is not the same as a dollar later. If money can earn a return, then today’s money has growth potential.
The future value formula is:
FV = PV * (1 + r)^n
Where:
-
PV= present value, the amount today -
r= rate per period -
n= number of periods -
FV= future value
This is similar to bodybuilding progress: one workout matters, but repeated training plus recovery compounds. Small rate differences become meaningful over many periods.
```cpp
#include <iostream>
#include <cmath>
#include <iomanip>
int main() {
double presentValue = 5000.0;
double annualRate = 0.06;
int years = 10;
double futureValue = presentValue * std::pow(1.0 + annualRate, years);
std::cout << std::fixed << std::setprecision(2);
std::cout << "$" << presentValue << " invested at "
<< annualRate * 100 << "% for " << years << " years becomes:\n";
std::cout << "$" << futureValue << "\n";
return 0;
}
```
Compounding Check
True or false:
With a positive interest rate, compounding over more periods generally increases the future value of an investment.
Cash Flow Timeline
Finance is time-indexed money
Many finance problems are not just one amount growing. They are sequences of cash flows:
Year: 0 1 2 3
Cash: -1000 300 400 500
A negative cash flow usually means money goes out. A positive cash flow means money comes in.
This structure appears everywhere: bonds, loans, projects, subscriptions, and portfolio income. For a web dev analogy, it’s like an array of events — but every event is tied to time and value.

Present Value
Discount future cash into today’s dollars
Future money is usually worth less than money today because today’s money can be invested.
Present value reverses compounding:
PV = FV / (1 + r)^n
For a sequence of cash flows, we discount each one and add them together. That sum is called net present value, or NPV.
NPV is a basic valuation tool. It asks: “After accounting for time and return expectations, what is this stream of money worth today?”
```cpp
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
int main() {
std::vector<double> cashFlows = {-1000.0, 300.0, 400.0, 500.0};
double discountRate = 0.08;
double npv = 0.0;
for (int year = 0; year < static_cast<int>(cashFlows.size()); year++) {
double presentValue = cashFlows[year] / std::pow(1.0 + discountRate, year);
npv += presentValue;
}
std::cout << std::fixed << std::setprecision(2);
std::cout << "Discount rate: " << discountRate * 100 << "%\n";
std::cout << "NPV: $" << npv << "\n";
if (npv > 0) {
std::cout << "This project creates value under these assumptions.\n";
} else {
std::cout << "This project does not create value under these assumptions.\n";
}
return 0;
}
```
Basis Point
Fill in the blank:
In finance code, 1 basis point equals ____ as a decimal rate.
Precision Matters
Rates, percentages, and basis points
Finance teams often talk in basis points, abbreviated bps.
1% = 100 bps
0.5% = 50 bps
0.01% = 1 bp
Why use bps? Because it reduces ambiguity. Saying “rates increased by 1%” could mean:
- from 5% to 6%, a one percentage-point increase
- from 5% to 5.05%, a 1% relative increase
Saying “rates increased by 100 bps” is clearer.
As you write C++ finance code, be explicit about whether a value is a decimal rate, a percent, or basis points. This is the kind of small detail that prevents expensive bugs.

NPV Meaning
A project has a positive NPV using your chosen discount rate.
What does that usually suggest?
Finance Foundation
What you can now express in C++
You’ve moved from general C++ syntax into finance-specific modeling:
- amounts of money with clear variable names
- rates as decimals, percents, and basis points
- simple interest and compound growth
- present value and future value
- cash flow timelines
- NPV as a basic valuation calculation
The key mindset: finance code is not just math. It is math plus assumptions, units, time, and precision.
Next, we’ll use this foundation to discuss how C++ can support algorithmic trading workflows — without losing sight of the basic finance calculations that power the decisions.
