18. Write a VB program to check whether given number is perfect or not by using ‘msgbox’.


Private Sub cmdClear_Click()
Text1.Text = “”
Text1.SetFocus
End Sub

Private Sub cmdExit_Click()
Unload Me
End Sub

Private Sub cmdfind_Click()
Dim n As Integer
Dim s As Integer
s = 0
n = Val(Text1.Text)
For i = 1 To n – 1
If n Mod i = 0 Then
s = s + i
End If
Next
If n = s Then
MsgBox “Perfect Number”, vbInformation, “Perfect”
Else
MsgBox “Not Perfect Number”, vbInformation, “Perfect”
End If

End Sub

Private Sub Form_Activate()
Text1.SetFocus
End Sub

One thought on “18. Write a VB program to check whether given number is perfect or not by using ‘msgbox’.

  1. MattH says:

    Instead of
    ——————————————————-
    For i = 1 To n – 1
    If n Mod i = 0 Then
    s = s + i
    End If
    Next
    ——————————————————-
    The below is MUCH faster, especially if you’re testing for ALL natural numbers, not just one value
    ——————————————————-
    x = n
    i = 1
    Do Until i > x
    If n Mod i = 0 Then
    If s = 0 Then
    s = i
    Else
    s = s + i + n / i
    End If
    End If
    i = i + 1
    x = n / i
    Loop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s