TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Odd and Even Numbers

This C# article tests for odd numbers with a simple method. It uses modulo division.

Odd, even. Odd numbers are not even.

With modulo division, we can see if the number is evenly divisible by two. If it is not, it must be odd. In this example, we demonstrate the IsOdd method and then the IsEven method.

Modulo Operator

Odd. The IsOdd static method performs a modulo division on the parameter, which returns the remainder of a division operation. If the remainder not 0, then the number must be odd—the remainder would be 0 if it was divisible by two.

Static Method

C# program that finds odd numbers

using System;

class Program
{
    static void Main()
    {
	for (int i = 0; i <= 100; i++)
	{
	    if (IsOdd(i))
	    {
		Console.WriteLine(i);
	    }
	}
    }

    public static bool IsOdd(int value)
    {
	return value % 2 != 0;
    }
}

Output

1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99

You could implement IsOdd by using the IsEven method and returning its boolean opposite. In other words, you could simply "return !IsEven(value)". For the source code to the IsEven method, check out the next section.

Correction: There was something odd about the first implementation of IsOdd on this page. It did not handle negative numbers correctly.

And: Fortunately, Joshua Goodman wrote in with the bug report and I corrected the problem.

Even. All even numbers are divisible by two. This means we can use the modulo division operator to see if there is any remainder when the number is divided by two. If there is no remainder (it returns zero) then the number is definitely even.

C# program that finds even numbers

using System;

class Program
{
    static void Main()
    {
	for (int i = 0; i <= 100; i++)
	{
	    if (IsEven(i))
	    {
		Console.WriteLine(i);
	    }
	}
    }

    public static bool IsEven(int value)
    {
	return value % 2 == 0;
    }
}

Output

0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

The IsEven method shown here is very simple. It could be inlined into the locations you call it without too much loss of clarity. To learn more about the parity of zero, check out Wikipedia.

Parity of zero: Wikipedia

Summary. A surprising number of programs require that you test for even and odd numbers. For example, if data must be entered in pairs, it will always be even. And if it is odd, an error would have occurred.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf