berikut contoh potongan kodenya :
Bilangan Genap atau Ganjil
Dim a As Integer
a = 12 'contoh
If a Mod 2 = 0 Then
MsgBox "Nilai a adalah Genap"
Else
MsgBox "Nilai a adalah Ganjil"
End If
a = 12 'contoh
If a Mod 2 = 0 Then
MsgBox "Nilai a adalah Genap"
Else
MsgBox "Nilai a adalah Ganjil"
End If
Bilangan Prima atau tidak
Ketikkan fungsi berikut di bagian '(Declarations)' dari Form.
Function Prima(ByVal Angka As Integer) As Boolean
If Angka < 2 Then Exit Function
Dim i As Integer
Prima = True
For i = 2 To (Angka - 1)
If Angka Mod i = 0 Then
Prima = False
Exit For
End If
Next
End Function
If Angka < 2 Then Exit Function
Dim i As Integer
Prima = True
For i = 2 To (Angka - 1)
If Angka Mod i = 0 Then
Prima = False
Exit For
End If
Next
End Function
Contoh penggunaan
Dim a As Integer
a = 11 'contoh
If Prima(a) Then
MsgBox "Nilai a adalah Prima"
Else
MsgBox "Nilai a adalah Tidak Prima"
End If
a = 11 'contoh
If Prima(a) Then
MsgBox "Nilai a adalah Prima"
Else
MsgBox "Nilai a adalah Tidak Prima"
End If
Tidak ada komentar:
Posting Komentar