Loop Question

Dear All,

Is it possible make below NPV calculation automatically? I mean can we use any loop for the following;

NPV= C0 + (C1/(1+r)) + (C2/(pow(1+r, 2))) + (C3/(pow(1+r, 3))) + (C4/(pow(1+r, 4)));
cout<<NPV<<'\n';

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// NPV calculation

#include<iostream>
#include<cmath>
#define r 0.12 //12% opportunity cos of capital

using namespace std;


int main()
{

//variables declaration
const short C0=-2000;  //initial investment
unsigned short C1=500; // cash inflow in year 1
unsigned short C2(225); // cash inflow in year 2
unsigned short C3=336; // cash inflow in year 3
unsigned short C4(40); // cash inflow in year 4
float NPV;  // NPV=?

	// Let s put the formula of NPV for 4 years: NPV=Co + C1/(1+r) + C2/(1+r)^2 + C3/(+r)^3 + C4/(1+r)^4
	
	NPV= C0 + (C1/(1+r)) + (C2/(pow(1+r, 2))) + (C3/(pow(1+r, 3))) + (C4/(pow(1+r, 4)));
	cout<<NPV<<'\n';


cin.get();
return 0;
}


I would appreciate any help,

Thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const unsigned int N = 5;
short C[N];

C[0] = -2000;
C[1] = 500;
C[2] = 225;
C[3] = 336;
C[4] = 40;

float NPV = 0;
for( int i = 0; i < N; i++ )
    NPV += C[i] / pow(1+r, i);

cout << NPV;
Topic archived. No new replies allowed.