Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENSE504 Introduction to Computing

Lab 4. Looping

Lab 4. If Statement and Looping

Lab Objectives:

To gain experience with

•  experience with using if statements on the PC.

•   using the for, while and do loop statements on the PC      Use the Visual Studio program template for these programs.

1. Car Insurance Discount (Visual Studio)

Specification

Some insurance companies offer discounts on car insurance premiums depending on the number of years the driver has had a driver's licence and the number of claims the driver has made in the last five years.

In this program, the user inputs (as integers)

years

claims

the number of years the driver has had a driver's license

the number of insurance claims made in the last five years

The (integer) percentage of the "standard premium" that will be charged is initially calculated as follows:

•   if years < 5 then percentage = 100 – 10 * years + 20 * claims

•  otherwise ("else") percentage = 50 + 20 * claims

The percentage calculated in this way is then further adjusted as follows:

•   if  percentage > 150, then insurance is refused

•  otherwise, if the percentage is between 100% and 150% then set percentage = 100

•   (otherwise, the percentage isn't adjusted)

Program Design

•   Display the program title.

•   Define any necessary variables.

•  Get the input values (year and claims).

•   Perform the calculation of the percentage value, and display the answer.

•   Finally, display the end of program message.

Typical Program Output

(user input is shown in bold)

CAR INSURANCE PREMIUM

Enter years licensed: 2

Enter number of claims: 0

Premium is 80% of standard premium.

CAR INSURANCE PREMIUM

Enter years licensed: 8

Enter number of claims: 0

Premium is 50% of the standard premium.

CAR INSURANCE PREMIUM

Enter years licensed: 8

Enter number of claims: 5

Premium is 100% of the standard premium.

CAR INSURANCE PREMIUM

Enter years licensed: 4

Enter number of claims: 5

INSURANCE IS REFUSED

2. Single Multiplication Table (Visual Studio)

Specification

Display a multiplication table for an integer value entered by the program user.

The user enters a number and the program displays the result after multiplying by all integer numbers from zero to ten inclusive.

Sample Output

(User input is shown underlined.)

TIMES TABLE

Enter a whole number: 8

The 8 times table is:

0 x 8 = 0

1 x 8 = 8

2 x 8 = 16

3 x 8 = 24

4 x 8 = 32

5 x 8 = 40

6 x 8 = 48

7 x 8 = 56

8 x 8 = 64

9 x 8 = 72

10 x 8 = 80

End of the 8 times table.

Program Design

•   Define any necessary variables.

•   Display the program title.

•  Get the input value (any integer number).

•   Display the table heading.

•   Use a loop to calculate and display each line of the table.

•   Finally, display the end message.


3. Two Dimensional Multiplication Table

Specification

Display all the multiplication tables for integers from 1 to 15.

This program has no user input.

Use formatting to get the numbers lined up correctly.

Use TWO nested loops to display this table.