Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to make a case which controls if the number i put in my variable is divisible by 2,3,4,5,6,7,8,9 en 10.

how should I write my case?
The number I put in shouldn't be a decimal

this is what I have at the moment, doesn't work (i'm dutch)

Dim getal as Integer
        Dim uitkomst As Integer
        Dim btn As Integer
        getal = tbgetal.text
        btn= uitkomst
        Select Case getal
            Case getal/2
                uitkomst= getal/2
            Case getal/3
                uitkomst= getal/3
        End Select
        tbuitkomst.text= uitkomst


edit: the assignment
CSS
Assignment 2
Input: a number, no decimal number.
Determine whether the number is divisible by 2,3,4,5,6,7,8,9 and 10.
Show your results in the form itself.
Tip: use a case!
Posted
Updated 27-Nov-10 22:20pm
v2

SELECT CASE on MSDN[^] read through this to see how it works.
 
Share this answer
 
I've written a function to check the divisibility of a number.
For me, this is the best solution:

VB
Public Function CheckDiv(ByVal N As Integer) As List(Of Integer)

    'list containing the results
    Dim RetList As New List(Of Integer)

    'using a for to check the divisibility of N
    For I As Integer = 2 To 10
        If N Mod I = 0 Then
            RetList.Add(I)
        End If
    Next

    'returns the list
    Return RetList

End Function


Using a for, we check if a number is divisible by 2,3,...,10. The mod operator returns the rest of a division. If it's 0, N is divisible by I.

The solution with the case is ugly; I advice you to use the first one! However, here it is:

VB
 Public Function CheckDiv(ByVal N As Integer) As List(Of Integer)

    'returning list
    Dim Ret As New List(Of Integer)

    'check the divisors
    For I As Integer = 2 To 10
        If N Mod I = 0 Then
            Select Case I
                Case 2
                    Ret.Add(2)
                Case 3
                    Ret.Add(3)
                Case 4
                    Ret.Add(4)
                Case 5
                    Ret.Add(5)
                Case 6
                    Ret.Add(6)
                Case 7
                    Ret.Add(7)
                Case 8
                    Ret.Add(8)
                Case 9
                    Ret.Add(9)
                Case 10
                    Ret.Add(10)
            End Select
        End If
    Next

    'returns the list
    Return Ret

End Function


Hope this helped!
 
Share this answer
 
In for loop divide it by %;
Example
VB
For I As Integer = 2 To 10
if (j%i==0)

then its an even number
j is an loop variable in for loop. i is the input that you have.

The % operator will give the remainder. If the remainder is 0 that means its an even number then you can display the number. Thats it.
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900