COMP2243 - Programming and Problem Solving

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Repetitions/Loops WhileLoop.java LoopWithCounter.java LoopWithSentinel.java LoopWithFlag.java SimpleInterest.java Factorial.java Fibnonacci.java PrimeNumber.java GCD.java PopulationGrowth.java ForLoop.java DoWhileLoop.java FileIO_Tickets.java FileIO_Salary.java NestedLoop.java



SimpleInterest Program


Description

A simple interest on a loan is calculated by the following formula.

interest = principal x annual interest rate x time period in days / 365

This program prompts the user to enter the principal, annual interest rate, time period in days for several loans, and calculates the simple interest charge for each loan.

The number of loans is unknown. So a sentinel-controlled loop is used to control the loop execution.

Associated Concepts:

Sentinel-controlled while loop

Algorithm:

Input the principal amount before entering the loop body

Use a sentinel controlled while loop for each iteration (use -1 as the sentinel value)
(If the principal amount is not equal to the sentinel value, the loop continues.)
(If the principal amount is equal to the sentinel value, the loop ends.)

    In the loop body, input the interest rate and the time period in days

    If the inputs are invalid

        Print an error message

    Otherwise

        Calculate and print the interest

    Input another principal amount at the end of the while loop body

Source Code



Sample Run



Video