Вы находитесь на странице: 1из 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.

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 \$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

## 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)

end if
'button click

## dim rateofpay,grosspay as double

double.tryparse(textbox1.text,hourswkd)

double.tryparse(textbox2.text,rateofpay)

grosspay(hourswkd,rateofpay,grosspay)

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

return area

end function

'button click

## dim circlearea, circleradius as double

double.tryparse(textbox1.text , circleradius)

circlearea = getcirclearea(circleradius)

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

## dim userhours as double

userhous = val(txthours.text)

wage = val(txtwage.text)

displaypay(userhours, wage)

## earnings = hours * rate

else
earnings = HOUR_LIMIT * rate

end if

end sub

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)

'clock timer

'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

## 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

## [!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

## Нижнее меню

### Получите наши бесплатные приложения

Авторское право © 2021 Scribd Inc.