SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Flow Of Control
Exercise:
Programming Projects
Question:4 | ISBN:9780132830317 | Edition: 5

Question

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, such as 5.6 (percent). Your program should then convert the percent to a fraction, such as 0.056, and should use a loop to estimate the price adjusted for inflation.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.Scanner;

public class TestDemo {

	public static void main(String[] args) {
		double cost, inflation;
		int years;
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the cost :");
		cost = input.nextDouble();
		System.out.print("Enter the number of years:");
		years = input.nextInt();
		System.out.print("Enter the inflation rate:");
		inflation = input.nextDouble() * .01;
		for (int i = 0; i < years; i++)
			cost += cost * inflation;
		cost = (int) (cost * 100) / 100.0;
		System.out.println("Item will cost about $ " + cost + " in " + years + " years. " );
	}
}

OUTPUT:

Enter the cost :2000
Enter the number of years:12
Enter the inflation rate:0.056
Item will cost about $ 2013.48 in 12 years. 

 

0 0

Discussions

Post the discussion to improve the above solution.