How the value of a car depreciates.
Watch this video to learn about the new concepts shown in the program:
Questions to think about with this program to check your understanding:
Explain the purpose of the iteration and condition in the while loop.
while (value >= resale_value && resale_value > 0)
The value of the car is recalculated until the value is less than the resale value and is also more than £0. The first condition stops when the resale value is reached and the second condition ensures the value cannot go below £0.
Explain why the final Console.WriteLine (“Part exchange before end of year …”) is outside the while block.
The final output statement about when to part exchange the car should not be inside the iteration because it needs to be executed after the year to part exchange has been calculated. This statement should only be output once, not after every calculation.
Change the program so that it:
Enter the value of the car purchased: £
8500
Enter the minimum part exchange value: £
2000
In year 0, the car is worth £8500
In year 1, the car is worth £6375
In year 2, the car is worth £4781
In year 3, the car is worth £3585
In year 4, the car is worth £2688
Part exchange before end of year 5
// Car value program
using System;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
static void show_value(int value, int resale_value)
{
---
int year = 0;
double depreciation = 0.25;
---
while ((value >= resale_value) && (resale_value > 0) && (year !=5))
{
---
Console.WriteLine($"In year {year}, the car is worth £{value}");
value = (int)(value - (value * depreciation));
year = year + 1;
---
}
---
Console.WriteLine($"Part exchange before end of year {year}");
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
Console.WriteLine("Enter the value of the car purchased: £");
int value = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the minimum part exchange value: £");
int resale_value = Convert.ToInt32(Console.ReadLine());
---
show_value(value, resale_value);
---
}
---
}