Thursday 13 December 2012

How to find odd and even numbers in VB.NET

How to find odd and even numbers in VB.NET

How to find odd and even numbers in Vb.NET

 

There are 2 methods find even or odd numbers inVB.NET

 

  Method 1: Modulus operator %

  Method 2:   BitField Operator &

Method 1:  Modulus Operator %

  Note:Find even or odd with modulus operator,When divided by 2 if remainder of the number is 0, then number is even otherwise odd.
    for  number 3  remainder is 1 so odd  3%2=1
    for number 4 remainder is 0 so even.  4 %2=0
Public Function isEven(ByVal num As Integer) As Boolean
           If num % 2 = 0 Then
                Return True
           Else If else Return False Then
           End If
 End Function
 

  Method 2:   BitField Operator &


        Public Function isEvenUsingBitFields(ByVal num As Int32) As Boolean
           If (num & 1) =0 Then
                Return True
           Else If else Return False Then
           End If
        End Function

 This examples uses bit operator And. If result is 0 then Even number, otherwise Odd Number.

No comments:

Post a Comment