Suppose that the tuition for a university is $10,000 this year and increases 5% every year

LANGUAGE: Java

CHALLENGE:

Suppose that the tuition for a university is $10,000 this year and increases 5% every year. In one year, the tuition will be $10,500. Write a program that displays the tuition in 10 years and the total cost of 4 years’ worth of tuition starting after the 10th year.

SOLUTION:

public class Exercise_1 {
    public static void main(String[] args) {
        int totalCost = 0;		// Accumulate total cost of four years tuition
        int tuition = 10000;
        int tuitionTenthYear;
    
        for (int year = 1; year <= 14; year++) {
            // Increase tuition by 5% every year
            tuition += (tuition * 0.05);  
    
            if (year > 10) // Test for after 10 years
                totalCost += tuition; // Accumulate tuition cost
    
            // Cost of tution in ten years
            if (year == 10)
                tuitionTenthYear = tuition; 
        }
    
        // Display the cost of tuition in ten years
        System.out.println("Tuition in ten years: $" + tuitionTenthYear);
    
        // Display the cost of four years' worth of tuition after tenth year
        System.out.println("Total cost for four years' worth of tuition" +
            " after the tenth year: $" + totalCost);
    }
}