Console application to check Even or Odd Number

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


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")
Else
Console.WriteLine("Odd number")
End If
Else
Console.WriteLine(str & " is not a Number.")
End If
Console.ReadLine()
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:
http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-check-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
else
total -= 1; //odd number


Definitely worth a read for those that are curious.

Login to post response