Console application to check Even or Odd Number

Posted by Chikul under VB.NET category on | Views : 12987

Module Module1

Sub Main()
Dim str As String, i As Integer
Console.WriteLine("Enter a Number :")
str = Console.ReadLine()
If IsNumeric(str) Then
i = CInt(str)
If str Mod 2 = 0 Then
Console.WriteLine("Even number")
Console.WriteLine("Odd number")
End If
Console.WriteLine(str & " is not a Number.")
End If
End Sub

End Module

Comments or Responses

Posted by: Firemyst on: 1/23/2014 Level:Starter | Status: [Member] | Points: 10

Here’s a blog article which benchmarks quite a few ways to test if a number is odd or even:

Surprisingly, the fastest way appears to be the modulus % operator, even out performing the bitwise ampersand &, as follows:

if (x % 2 == 0)
total += 1; //even number
total -= 1; //odd number

Definitely worth a read for those that are curious.

Login to post response