Sei sulla pagina 1di 8

sample problems

1. Write a program which allows the user to enter their age. The program should then print either "You are
still young" or "You are getting old" depending on whether the person's age is greater than 100 or not.

2. Write a program in which the user can enter two numbers (integers). If both numbers are zero
then print the message "Two zeros". If the numbers are not both zero, then print either "FIRST" or
"SECOND" depending on which number is bigger. If the numbers are the same (but not both zero)
then print "EQUAL"
3. Write a program which allows the user to enter a number of pollution readings (integers). As
each pollution reading is entered the program should test the reading and print one of the following
messages:
Comfortable (for readings less than 30)
Moderate (for readings less from 30 to 60 inclusive)
Dangerous (for readings over 60)
4. Write a program in which the user enters their salary. The program is to calculate and display
the amount of tax payable on this salary. Use the (simple) tax rule: Salaries under $4000 pay 2%
tax, salaries of $4000 or more pay 65% tax (!)

loop sample problems


1. Write a program which uses a while loop to print the
numbers from 200 to 1200 inclusive, on the screen.

2. Write a program which prints the multiples of 5 from 5 to


100. That is,

5 10 15 ... etc

3.Write a program which prints a sequence of triangular


numbers starting with 1 and ending with 36:

1 3 6 10 15 21 28 36

4. write a program that asks a user if he/she is cute. the


program will terminate if the user answers no. use a
messagebox

5. write a program that counts the number of characters


entered until the user answers 'yes'.

6. count and display the number of vowels entered by a user.


the program will end when the user answers 'yes'.
7. Write a program which allows the user to enter a number of
salaries. As each salary is entered, the tax payable on that
salary
and the net salary (after tax) should be calculated and
printed on the screen. The tax payable should also be
accumulated into a total.
When the user enters any salary which is less than zero,
the program should display the total tax and then terminate.

For the purpose of this problem, assume that tax is


calculated using the rules:

Salaries from $0 to $4000: no tax payable

Salaries from $4001 to $34999: tax at 30%

Salaries $35000 or above: tax at 48%

functions
private sub showmsg(byval pet as string, byval age as integer)

age+=1 label1.text = "your pet, " & pet & "will be " & age & "years old"

end sub

'button click event

dim name as string dim age as string

dim currentage name = inputbox("pet's name")

age = inputbox("age")

integer.tryparse(inputage,currentage)

call showmsg(name, currentage)

private sub grosspay(byval hours as double, byval rate as double, byref gross as double)

gross = hours * rate

if hours > 40 then

gross = gross + (hours -40) * rate /2

end if
'button click

dim hourswkd as double

dim rateofpay,grosspay as double

double.tryparse(textbox1.text,hourswkd)

double.tryparse(textbox2.text,rateofpay)

grosspay(hourswkd,rateofpay,grosspay)

lblGross.text = grosspay.tostring("C2")

private function getcirclearea(byval radius as double) as double

dim area as double area = 3.141593 * radius ^ 2

return area

end function

'button click

dim circlearea, circleradius as double

double.tryparse(textbox1.text , circleradius)

circlearea = getcirclearea(circleradius)

label1.text = circlearea.tostring("N2")

'calculate button event

dim userhours as double

userhous = val(txthours.text)

wage = val(txtwage.text)

displaypay(userhours, wage)

sub displaypay(byval hours as double, byval as rate as decimal)

dim earnings as decimal

const HOUR_LIMIT as integer = 40

if checkovertime(hours,HOUR_LIMIT) = False then

earnings = hours * rate

else
earnings = HOUR_LIMIT * rate

earnings += ((hours - HOUR_LIMIT) * (1.5 * rate))

end if

label1.text = string.format("{0:C}", earnings)

end sub

Function checkovertime(byval total as double, byval limit as integer) as boolean

if total > limit

return true

else return false

end if

end function

Function BoxVolume( Optional ByVal length As Integer = 1,Optional ByVal width As Integer = 1,
Optional ByVal height As Integer = 1 ) As Integer

Return length * width * height

End Function

'button click

label1.text = BoxVolume()

label2.text =BoxVolume(10)

label3.text =BoxVolume(10, 20)

label4.text =BoxVolume(10, 20, 30)

label5.text =BoxVolume(, 20, 30)

label6.text =BoxVolume(10, , 30)

dim delivery as date = new date(2014,10,29,0,0,0)

'clock timer

label1.text = string.format("{0:hh:mm:ss tt)", date.now)

'square root

Private Function sqroot(ByRef x As Single) As Double


x = x ^ 0.5
sqroot = x
End Function
Private Function sqroot1(ByVal y As Single) As Double
y = y ^ 0.5
sqroot1 = y
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim u As Single
u=9
MsgBox(3 * sqroot(u), , ByRef)
MsgBox(Value of u is & u, , ByRef)

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


Dim u As Single
u=9
MsgBox(3 * sqroot1(u), , ByVal)
MsgBox(Value of u is & u, , ByVal)
End Sub

manipulating strings
'determining number of characters in a string
Dim strname As String
Dim intnumchars As Integer

strname = TextBox1.Text
intnumchars = strname.Length

MsgBox(intnumchars)

'prompt user to enter 5 character code

Do
strname = InputBox("enter 5 letter code", "code")
Loop Until strname.length = 5

removing characters from a string


'trim - remove space characters at beginning and and end of string

strname = TextBox1.Text.Trim
strname = strname.Trim
Label1.Text = strname

'string,remove

strname = TextBox1.Text
'strname = strname.Remove(0, 4)
strname = strname.Remove(4)
Label1.Text = strname

'add 5 character string to listbox

Dim strid As String


strid = TextBox1.Text.Trim

If strid.Length = 5 Then
ListBox1.Items.Add(strid.ToUpper)
Else
MessageBox.Show("the id must contain 5 characters", "id", MessageBoxButtons.OK,
MessageBoxIcon.Information)

End If

'padding strings

strname = TextBox1.Text
'strname = strname.PadLeft(5)
'strname = strname.PadRight(10)
strname = strname.PadLeft(5, "*")
Label1.Text = strname

'searching a string

Dim intname As Integer

strname = TextBox1.Text
If strname.ToUpper.Contains("ELM ST.") Then
MsgBox("string found")

End If
Label1.Text = strname.Contains("In")

intname = strname.IndexOf("ID")
Label1.Text = intname

Accessing characters in a string


Dim strfirst, strlast As String

strname = TextBox1.Text
strfirst = strname.Substring(0, 4)
strlast = strname.Substring(5)
Label1.Text = strfirst
'Label1.Text = strlast

'rearrange name

Dim strfirst, strlast As String


Dim intindex As Integer
strname = TextBox1.Text.Trim
intindex = strname.IndexOf(" ")
If intindex <> -1 Then
strfirst = strname.Substring(0, intindex)
strlast = strname.Substring(intindex + 1)
Label1.Text = strlast & "," & strfirst

Else
MessageBox.show("invalid name format", "rearrange name", MessageBoxButtons.OK,
MessageBoxIcon.Error)

End If

'pattern matching to compare strings

LIKE operator allows you to use pattern matching characters to determine whether one string is
equal to another string

Pattern matching characters

? (any single character)

* (zero or more characters)

# (any single digit)

[charlist] (match any single character)

[!charlist] not any character in the character list

strname = TextBox1.Text
If strname Like "#####?" Then
MessageBox.Show("string matches")
Else
MessageBox.Show("string does not match")
End If

'check id format

Dim strid As String


strid = TextBox1.Text.Trim.ToUpper
If strid Like "[A-Z][A-Z][A-Z]##" Then
ListBox1.Items.Add(strid)
Else
MessageBox.Show("invalid id format", "invalid id", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

Replace strings with string.replace

strname = TextBox1.Text
TextBox1.Text = strname.Replace("old", "new")

strname = TextBox1.Text
'replace string

If strname Like "f?ck" Or strname Like "g*g*" Then


strname = strname.Replace(strname, "bawal yan")
End If

TextBox1.Text = strname

'using mid function

strname = TextBox1.Text
Mid(strname, 1) = "ok"
TextBox1.Text = strname

Potrebbero piacerti anche