You are on page 1of 5

CONTROL STRUCTURE EXERCISE

1. Body Mass Index (BMI) is a measure of health based on height and weight. It can be
calculated by taking your weight in kilograms and dividing it by the square of your height
in meters. The interpretation of BMI for people 20 years or older is as follows:
BMI Interpretation
BMI < 18.5 Underweight
18.5 ≤ BMI < 25.0 Normal
25.0 ≤ BMI < 30.0 Overweight
30.0 ≤ BMI Obese
Write a program that prompts the user to enter a weight in pounds and height in inches
and displays the BMI. Note that one pound is 0.45359237 kilograms and one inch is
0.0254 meters.
Imports System

Module BMICalculator
Sub Main()

Console.Write("Enter your weight in pounds: ")


Dim weightInPounds As Double = CDbl(Console.ReadLine())

Console.Write("Enter your height in inches: ")


Dim heightInInches As Double = CDbl(Console.ReadLine())

Dim weightInKilograms As Double = weightInPounds * 0.45359237


Dim heightInMeters As Double = heightInInches * 0.0254
Dim bmi As Double = weightInKilograms / (heightInMeters * heightInMeters)

Console.WriteLine("Your BMI is: " & bmi)

If bmi < 18.5 Then


Console.WriteLine("Underweight")
ElseIf bmi < 25.0 Then
Console.WriteLine("Normal")
ElseIf bmi < 30.0 Then
Console.WriteLine("Overweight")
Else
Console.WriteLine("Obese")
End If

Console.ReadLine()
End Sub
End Module
2. Suppose you want to develop a program to play lottery. The program randomly generates
a lottery of a two-digit number, prompts the user to enter a two-digit number, and
determines whether the user wins according to the following rules:
o If the user input matches the lottery number in the exact order, the award is KSh.
10,000.
o If all digits in the user input match all digits in the lottery number, the award is
KSh. 3,000.
o If one digit in the user input matches a digit in the lottery number, the award is
KSh. 1,000.
Write a program implementing this requirement.

Imports System

Module Program
Sub Main()

Dim random As New Random()


Dim lotteryNumber As Integer = random.Next(10, 100)

Console.Write("Enter a two-digit number: ")


Dim userInput As Integer = Integer.Parse(Console.ReadLine())

Dim lotteryDigit1 As Integer = lotteryNumber \ 10


Dim lotteryDigit2 As Integer = lotteryNumber Mod 10
Dim userDigit1 As Integer = userInput \ 10
Dim userDigit2 As Integer = userInput Mod 10

If userInput = lotteryNumber Then


Console.WriteLine("Congratulations! You've won KSh. 10,000!")

ElseIf (userDigit1 = lotteryDigit1 Or userDigit1 = lotteryDigit2) And


(userDigit2 = lotteryDigit1 Or userDigit2 = lotteryDigit2) Then
Console.WriteLine("Congratulations! You've won KSh. 3,000!")

ElseIf userDigit1 = lotteryDigit1 Or userDigit1 = lotteryDigit2 Or


userDigit2 = lotteryDigit1 Or userDigit2 = lotteryDigit2 Then
Console.WriteLine("Congratulations! You've won KSh. 1,000!")
Else
Console.WriteLine("Sorry, you didn't win. Better luck next time!")
End If

Console.ReadLine()
End Sub
End Module
3. Suppose you save $100 each month into a savings account with the annual interest rate
5%. So, the monthly interest rate is 0.05 / 12 = 0.00417. After the first month, the value
in the account becomes100 * (1 + 0.00417) = 100.417. After the second month, the value
in the account becomes (100 + 100.417) * (1 + 0.00417) = 201.252. After the third
month, the value in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.507 and
so on. Write a program that prompts the user to enter an amount (e.g., 100), the annual
interest rate (e.g., 5), and the number of months (e.g., 6) and displays the amount in the
savings account after the given month.

Imports System

Module Program
Sub Main()
Console.WriteLine("Enter the initial amount:")
Dim amount As Double = Double.Parse(Console.ReadLine())

Console.WriteLine("Enter the annual interest rate:")


Dim annualInterestRate As Double = Double.Parse(Console.ReadLine()) / 100

Console.WriteLine("Enter the number of months:")


Dim numMonths As Integer = Integer.Parse(Console.ReadLine())

Dim monthlyInterestRate As Double = annualInterestRate / 12


Dim totalAmount As Double = amount

For i As Integer = 1 To numMonths


totalAmount += totalAmount * monthlyInterestRate
totalAmount += amount
Next

Console.WriteLine("The amount in the savings account after " & numMonths & " months is: " &
totalAmount.ToString("0.00"))
End Sub
End Module

4. Personal income tax is calculated based on filing status and taxable income earned as
shown in the table below. There are three filing statuses: single filers, married filing
jointly or qualified widow(er) and married filing separately. If you are, say, single with a
taxable income of $10,000, the first $8,350 is taxed at 10% and the other $1,650 is taxed
at 15%, so, your total tax is $1,082.50.
Write a program to compute personal income tax. Your program should prompt the user
to enter the filing status (Using select…case structure, enter 0 for single filers, 1 for
married filing jointly or qualified widow(er), 2 for married filing separately) and taxable
then compute personal income and display necessary details in a suitable format.

Imports System

Module PersonalIncomeTaxCalculator
Sub Main()
Console.WriteLine("Personal Income Tax Calculator")
Console.WriteLine("--------------------------------")

Console.WriteLine("Please enter the filing status:")


Console.WriteLine("0 - Single filer")
Console.WriteLine("1 - Married filing jointly or qualified widow(er)")
Console.WriteLine("2 - Married filing separately")
Console.Write("Enter the filing status (0, 1, or 2): ")
Dim filingStatus As Integer = Convert.ToInt32(Console.ReadLine())

Console.Write("Enter the taxable income: ")


Dim taxableIncome As Double = Convert.ToDouble(Console.ReadLine())

Dim tax As Double = 0.0

Select Case filingStatus


Case 0 ' Single filer
If taxableIncome <= 8350 Then
tax = taxableIncome * 0.1
Else
tax = 8350 * 0.1 + (taxableIncome - 8350) * 0.15
End If
Case 1 ' Married filing jointly or qualified widow
Console.WriteLine("Tax calculation for Married filing jointly or qualified widow(er) is not implemented yet.")
Case 2 ' Married filing separately
Console.WriteLine("Tax calculation for Married filing separately is not implemented yet.")
Case Else
Console.WriteLine("Invalid filing status.")
End Select

Console.WriteLine()
Console.WriteLine("Tax Details")
Console.WriteLine("Filing Status: " & filingStatus)
Console.WriteLine("Taxable Income: " & taxableIncome.ToString("C"))
Console.WriteLine("Tax Amount: " & tax.ToString("C"))

Console.ReadLine()
End Sub
End Module

5. Write a program that display the following patterns


&&&&&
&&* *&
&*& *&
&* *&&
&&&&&

|| ||
|| ||
|| || || || ||
|| ||
|| ||

Module MainModule
Sub Main()
DisplayPattern1()
Console.WriteLine()
DisplayPattern2()
Console.ReadLine()
End Sub

Sub DisplayPattern1()
For i As Integer = 1 To 5
For j As Integer = 1 To 5
If j = 3 And i <> 1 And i <> 5 Then
Console.Write("*")
Else
Console.Write("&")
End If
Next
Console.WriteLine()
Next
End Sub

Sub DisplayPattern2()
For i As Integer = 1 To 5
For j As Integer = 1 To 5
If i = 3 And (j = 3 Or j = 4) Then
Console.Write("||")
Else
Console.Write(" ")
End If
Next
Console.WriteLine()
Next
End Sub
End Module

You might also like