8. It is difficult to make a budget that spans several years, because prices are not stable. If your company needs 200 pencils per year,you cannot simply use this year’s price as the cost of pencils two years from now. Because of inflation the cost is likely to be higher than it is today. Write a program to gauge the expected cost of an item in a specified number of years. The program asks for the cost of the item, the number of years from now that the item will be purchased, and the rate of inflation. The program then outputs the estimated cost of the item after the specified period. Have the user enter the inflation rate as a percentage, like 5.6 (percent). Your program should then convert the percent to a fraction, like 0.056, and should use a loop to estimate the price adjusted for inflation. (Hint: This is similar to computing interest on a charge card account, which was discussed in this chapter.)
Program:
//Import files
#include<iostream>
using namespace std;
//Program starts
int main()
{
//Declaration of variables
double itemPrice;
double inflation;
int years;
char repeatOption;
do
{
//Program asks for the cost of the item,
//the number of years from now that the item will be purchased,
//and the rate of inflation
cout<<"Please enter the cost of the item: ";
cin>>itemPrice;
cout<<"Please the number of years from now that the item will be purchased: ";
cin>>years;
cout<<"Please enter the rate of inflation: ";
cin>>inflation;
//Calculate the estimated cost of the item
//after the specified period and print output
inflation/= 100;
for (int i=0; i<years; i++)
{
itemPrice *= (1 + inflation);
}
cout<<"Estimated cost of item after "<<years
<<" will be: "<<itemPrice<<endl;
cout<<"\n\nType ('Y' or 'y') for repeat calculations:";
cin>>repeatOption;
}while(repeatOption == 'y' || repeatOption == 'Y');
}
Output of the program:
Please enter the cost of the item: 595.25
Please the number of years from now that the item will be purchased: 12
Please enter the rate of inflation: 25.5
Estimated cost of item after 12 will be: 9087.08
Type ('Y' or 'y') for repeat calculations: y
Please enter the cost of the item: 900
Please the number of years from now that the item will be purchased: 5
Please enter the rate of inflation: 100
Estimated cost of item after 5 will be: 28800
Type ('Y' or 'y') for repeat calculations: n