Professional Documents
Culture Documents
CSC123F Test1 2016 Memo
CSC123F Test1 2016 Memo
__________________________________________________________________________________
1. A number is called perfect if the sum of all the factors is equal to twice the number.
For example, 6 is perfect because 1 + 2 + 3 + 6 = 12 = 2*6.
A. Write down a function called Perfect that will accept a number as its argument
and return a string telling if the number is perfect or not. [7]
Ans
Public Function Perfect (ByVal num As Single) As String
Dim k, sum As Single
For k = 1 To num1
If num Mod k = 0 Then
sum += k
End If
Next
If sum = 2 * num Then
Return " is perfect"
Else
Return " is not Perfect"
End If
End Function
B. Write down a calling procedure for the function above that reads n from a text
box and prints out whether n is perfect or not in the message box. [3]
Ans:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
Handles Button1.Click
Dim n
n = TextBox1.Text
MsgBox(n & Perfect(n))
End Sub
1|Page
C. Terminate a program while running [2]
Ans
Exit Sub
Ans: Listbox shows all information at once while combo box provide you with a
dropdown menu to view items
Ans: Radio buttons are mutually exclusive while in check boxes you can select
multiple items
4. Write a program that will compute for you the area of a rectangle whose length and
breadth are read from text box1 and text box2 respectively, and print the area in text
box3. Your program should handle errors that might occur at run time using On Error
GoTo method as follows:
It must print a message Verify the values in you text boxes and show the word
Error in text box3.
Note the above messages should not be shown if everything goes well. [10]
Ans:
2|Page