Saving a value in a for loop.

It computes the value, but then i need it to save that value and compute it again with the new value.

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
  cout << "What is your deposit : ";
cin >> p;
cout << "Enter your annual interst rate : ";
cin >>r;
r = r / 100;
cout << "Your deposit : " << p << endl << "Your annual rate : " << r << "%" << endl;
cout << "How many years : ";
cin >> t;
cout << "How many compunds per year : ";
cin >> k;
	if (k != 1 and k != 12 and k != 365 and k != 366)
	{
		cout << "Error, compunds must be daily (365,366) , monthly (12) , annually (1)";
		return 1;
	}
	else
	{
		e = k * t;
		b = 1 + (r / k);
		for ( int i = 1; i <= t ; i++)
		{
			a= p * (pow((1+ r/k), e));
			cout << "Amount after "<< i << " year of interest : " << a << endl;
			
		}
	}
		
Since you're doing compound interest, you want to multiply a by pow((1+ r/k), e)) each time. Just set it a = p before the loop, and use a *= pow(b, e) inside. (not 100% don't quote me on this)
Topic archived. No new replies allowed.